Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WEB-INF/classes/ vs WEB-INF/lib/*.jar in classpath priority?

Tags:

tomcat

aspectj

a war packaged for a tomcat webapp contains WEB-INF/classes and WEB-INF/lib/*.jar

which of them has higher priority in the CLASSPATH?

the reason I'm asking is, that my application uses A.jar, which contains aspects generated from an aspectj project; and B.jar, which are to be woven with the aspects from A.jar. when the project myapp is compiled, it generates many Classes which override those same classes from B.jar, these are packaged into the WEB-INF/classes dir. so if tomcat load WEB-INF/lib/*.jar first, then the woven aspects won't take effect

like image 288
teddy teddy Avatar asked Dec 03 '11 01:12

teddy teddy


2 Answers

Tomcat has several documents on the class loader:

  • high-level: The Loader Component (https://tomcat.apache.org/tomcat-5.5-doc/config/loader.html)
  • details: Class Loader HOW-TO (https://tomcat.apache.org/tomcat-5.5-doc/class-loader-howto.html)

Quote from http://tomcat.apache.org/tomcat-5.5-doc/class-loader-howto.html:
(line breaks added for readability)

As mentioned above, the web application class loader diverges from the default Java 2 delegation model (in accordance with the recommendations in the Servlet Specification, version 2.3, section 9.7.2 Web Application Classloader).

When a request to load a class from the web application's WebappX class loader is processed, this class loader will look in the local repositories first, instead of delegating before looking.
There are exceptions. Classes which are part of the JRE base classes cannot be overriden. For some classes (such as the XML parser components in J2SE 1.4+), the J2SE 1.4 endorsed feature can be used (see the common classloader definition above).

Last, any JAR containing servlet API classes will be ignored by the classloader.

All other class loaders in Tomcat 5 follow the usual delegation pattern.

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
  • System class loader classes (described above)
  • /WEB-INF/classes of your web application
  • /WEB-INF/lib/*.jar of your web application
  • $CATALINA_HOME/common/classes
  • $CATALINA_HOME/common/endorsed/*.jar
  • $CATALINA_HOME/common/i18n/*.jar
  • $CATALINA_HOME/common/lib/*.jar
  • $CATALINA_BASE/shared/classes
  • $CATALINA_BASE/shared/lib/*.jar

So WEB-INF/classes is searched first before WEB-INF/lib.

like image 59
ziggy Avatar answered Nov 10 '22 01:11

ziggy


If you put classes into WEB-INF/classes, they have priority before the jars in WEB-INF/lib. I have sometimes used this for debugging purposes. See also here.

like image 5
user1078445 Avatar answered Nov 10 '22 00:11

user1078445