javatomcatjarjvmclassloader

class loading in tomcat on demand when 2 versions of a lib are present?


I was searching about this with many sources from the internet. But still I couldn't find a specific resource which exactly addresses my question.

In tomcat, is there any order in classes are loaded at startup. Just imagine a situation like this.

I have myApp.war file inside webapps. And inside that war, I have some library (say mylib-2.5.jar bundled inside it). And imagine, I have also placed the same library in different version (say mylib-2.8.jar) inside the /lib folder.

So when I start up my application, and call a particular end point, which lib (version) will have the chance to be loaded (on demand) into JVM by a class loader?

And what are the chances for a lib conflict? and what are the reasons for that?


Solution

  • From link posted by @GyroGearless

    Therefore, from the perspective of a web application, class or resource loading looks in the following repositories, in this order:

    •Bootstrap classes of your JVM

    •/WEB-INF/classes of your web application

    •/WEB-INF/lib/*.jar of your web application

    •System class loader classes (described above)

    •Common class loader classes (described above)

    If the web application class loader is configured with <Loader delegate="true"/> then the order becomes:

    •Bootstrap classes of your JVM

    •System class loader classes (described above)

    •Common class loader classes (described above)

    •/WEB-INF/classes of your web application

    •/WEB-INF/lib/*.jar of your web application

    If classloader needs to load class X then once it finds it in one place it will not look for it in any other places. Lib conflict is possible if you place one version of your dependency (say MySQL driver) to $JAVA_HOME/jre/lib/ext and another one into /WEB-INF/lib