Could anyone explain the order in which jar files are loaded from the lib directory within Tomcat? Is it alphabetically? Randomly? Or some other order?
If one JAR has version A of a class and another JAR has version B of that same class, then you will use version A if the first JAR is first and version B if the second is first.
If a JAR file is intended to be used as an executable file, the manifest file specifies the main class of the application. The manifest file is named MANIFEST. MF . The manifest directory has to be the first entry of the compressed archive.
As of Java 6, extension JAR files may also be placed in a location that is independent of any particular JRE, so that extensions can be shared by all JREs that are installed on a system. It says that for Windows you should place your extensions here %SystemRoot%\Sun\Java\lib\ext .
It's all described in Tomcat's ClassLoading HOW-TO. It's not necessarily in alphabetic order. If you observed that behaviour, it should absolutely not be relied upon if you intend to keep your webapp portable across servers. For example, Tomcat 6 "coincidentally" orders it, but Tomcat 8 does not.
Summarized, the loading order is as follows:
JRE/lib
, then server.loader
)WEB-INF/classes
, then WEB-INF/lib
)common.loader
, then Tomcat/lib
) shared.loader
)If you would like to guarantee that JAR X is loaded after JAR Y, then you'd need to put JAR X in one of the places which appears later in the listing above.
There are exceptions however, which are mentioned in the tomcat docs
Lastly, the web application class loader will always delegate first for JavaEE API classes for the specifications implemented by Tomcat (Servlet, JSP, EL, WebSocket). All other class loaders in Tomcat follow the usual delegation pattern.
That means if a webapp contains any JavaEE classes (javax.*
), then it will be ignored by tomcat.
For each loader, the classes are just loaded by the JVM in the order whenever they needs to be imported/executed and are not loaded yet.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With