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?
-
1Similar to: http://stackoverflow.com/q/8364619/435605 – AlikElzin-kilaka Feb 24 '15 at 09:43
5 Answers
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:
- bootstrap/system (
JRE/lib
, thenserver.loader
) - webapp libraries (
WEB-INF/classes
, thenWEB-INF/lib
) - common libraries (
common.loader
, thenTomcat/lib
) - 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.
-
2Does it explain the order from which the files *in a single directory* are loaded? – Joachim Sauer Mar 29 '11 at 15:18
-
1@Joachim: It's just loaded at the point the JVM needs it. The OP was more asking for search order. – BalusC Mar 29 '11 at 15:20
-
Thanks BalusC. I will check the order in which artifacts are needed and hopefully will be able to come to a reasonable solution – Damien Mar 29 '11 at 15:31
-
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. – BalusC Mar 29 '11 at 15:32
-
that link doesn't mention shared.loader [apparently it defaults to just common in tomcat 6 http://stackoverflow.com/a/6258314/32453 but if you do define one, it is used before common AFAICT so 4 should come before 3?] – rogerdpack Sep 26 '14 at 17:57
-
"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? – sb4 Sep 23 '20 at 15:19
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.

- 916
- 2
- 15
- 32

- 369
- 3
- 2
-
6
-
11I can confirm this for Tomcat 5+6, but currently have problems because Tomcat 8.0.9 does NOT behave like that! – Daniel Jul 13 '14 at 05:47
-
1The 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. – Christopher Schultz Jun 01 '20 at 20:51
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.

- 291
- 3
- 5
-
1
-
-
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](http://stackoverflow.com/help/how-to-answer) to see how you can improve your answer. – Luís Cruz Nov 04 '14 at 16:00
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

- 564
- 2
- 11
In our WebSphere env, different machine loads the jar in different order. So I agree with user2193008. We encounter class loader issue in Production env where same code works fine in lower env. We fixed issue by removing duplicate jar in lib, for example, there are two version of spring jars, spring_v1.jar and spring_v2.jar, if classloader loads v1 first, code works fine, but if classloader loads v2 first, trouble.

- 1,698
- 1
- 17
- 20