Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
142 views
in Technique[技术] by (71.8m points)

java - Order of loading jar files from lib directory

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?

question from:https://stackoverflow.com/questions/5474765/order-of-loading-jar-files-from-lib-directory

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...