Order of loading jar files from lib directory

48,221

Solution 1

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:

  1. bootstrap/system (JRE/lib, then server.loader)
  2. webapp libraries (WEB-INF/classes, then WEB-INF/lib)
  3. common libraries (common.loader, then Tomcat/lib)
  4. webapp-shared libraries (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.

Solution 2

Actually, it is alphabetical order! (Within a specific directory, e.g. the 'lib' directory that the original poster mentions.)

More specifically, if you look at the source for Tomcat 6, in class FileDirContext, the method list() calls Arrays.sort() on the array of file names of jars that are found.

I have tested this manually as well. I create a war with a JSP that calls HelloWorld.getGreeting(), and put two nearly identical jars containing slightly different versions of HelloWorld into the WEB-INF/lib directory. One says "Hello, world", the other "Goodbye, cruel world".

If I name the "Hello, world" version a.jar, and the "goodbye" version b.jar, and restart Tomcat, I get the "Hello" text. If I name the jars the other 'way round and restart Tomcat, I get the "Goodbye" text.

As far as I have been able to determine, this behaviour is NOT documented, NOT specified, and should NOT be relied on. But it is alphabetical -- for now.

Solution 3

Order for loaded jars in WEb-INF/lib folder.

For tomcat 5-7 the order is alphabetical. It uses sort.

For tomcat 8 is random decided by underlying file system.

https://issues.apache.org/bugzilla/show_bug.cgi?id=57129

Solution 4

I totally agree to the answer provided by BalusC. Only point to add is when it comes to loading a JAR from respective classloader it does check alphabetical order.

I had a situation to load "XSSFWorkbook" in my web application to read data from excel sheet and had two JARS "poi-ooxml-3.9.jar" and "org.eclipse.birt.runtime_4.3.1.v20130918-1142.jar" with same class name and package structure. I simply remaned the later so in its alphabetical order comes next to the earlier one and it did a trick for me. Just wanted to share the same.

Good Luck

Share:
48,221
klye_g
Author by

klye_g

Updated on July 09, 2022

Comments

  • klye_g
    klye_g almost 2 years

    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?

  • Joachim Sauer
    Joachim Sauer about 13 years
    Does it explain the order from which the files in a single directory are loaded?
  • BalusC
    BalusC about 13 years
    @Joachim: It's just loaded at the point the JVM needs it. The OP was more asking for search order.
  • klye_g
    klye_g about 13 years
    Thanks BalusC. I will check the order in which artifacts are needed and hopefully will be able to come to a reasonable solution
  • BalusC
    BalusC about 13 years
    In the future, it's better to post a clear and concise question about the concrete problem (e.g. "I have x.jar in place X, yet I get NoClassDefFoundError, blahblah") instead of a (broad) question about the subject of which you think that it's the root cause of the problem in question.
  • bakoyaro
    bakoyaro over 12 years
    +1 for pointing out a "gotcha" with the published documentation
  • Daniel
    Daniel almost 10 years
    I can confirm this for Tomcat 5+6, but currently have problems because Tomcat 8.0.9 does NOT behave like that!
  • rogerdpack
    rogerdpack over 9 years
    that link doesn't mention shared.loader [apparently it defaults to just common in tomcat 6 stackoverflow.com/a/6258314/32453 but if you do define one, it is used before common AFAICT so 4 should come before 3?]
  • Luís Cruz
    Luís Cruz over 9 years
    Are you answering or asking a new question?
  • user2193008
    user2193008 over 9 years
    Take a guess :). I was answering.
  • Luís Cruz
    Luís Cruz over 9 years
    Please take some time to reformat your answer in order to make it look like an answer. Take a look at How do I write a good answer to see how you can improve your answer.
  • Christopher Schultz
    Christopher Schultz almost 4 years
    The alphabetical ordering of Tomcat 6 should not be relied upon. It is not a part of the Servlet specification, and relying on such behavior should be considered contrary to the specification. Later versions of Tomcat do not order alphabetically, so you definitely do not want to rely on that behavior.
  • sb4
    sb4 over 3 years
    "That means if a webapp contains any JavaEE classes (javax.*), then it will be ignored by tomcat." That's grammatically confusing -- "it" (the webapp) will be ignored (grammatical implication)? "It" (the jar) will be ignored? "It" (a class in the javax package) will be ignored?