Fred Porciúncula
Fred Porciúncula

Reputation: 8902

Tomcat 8 classloading - difference between JAR in [WEB-INF/lib] and [tomcat/lib]

It says here that the common classloader is visible to all web application. So what is the difference between having a JAR file within the WEB-INF/lib folder of my war application and having the same JAR file within Tomcat's lib folder?

This JAR is a provider for a Java SPI that I created. When I have it under WEB-INF/lib, everything works perfectly. But if I try to put this JAR under Tomcat's lib folder (or under shared/lib after configuring it in catalina.properties) I get errors. The second option is better for me because I need total decoupling of my application and the provider.

The error I get is a ClassNotFoundException for the interface that represents my service (which the JAR implements). This interface is in a third project, which is a dependency for my war application.

Upvotes: 4

Views: 5026

Answers (2)

blirp
blirp

Reputation: 41

The difference is the classloader being used. Tomcat has very specific rules for it's classloaders, some of them are counter intuitive. Your problem here, is that a class loaded in one classloader is not compatible with a class loaded in another. Even Object to Object will fail.

The jar containing your interface is loaded by the webapp classloader since it is located in the war-file. The implementation is loaded by the common classloader. This does not contain your interface, so it throws a ClassNotFoundException.

The solution is to move all jars into the same classloader. Either everything in the lib-folder, or everything in your war.

Implementing a plug-in architecture with Tomcat is a rather difficult undertaking. Using something like OSGi would probably help. But for small problems, it's probably overkill. Fun, but overkill.

Upvotes: 4

Crazyjavahacking
Crazyjavahacking

Reputation: 9677

The tomcat/lib directory should be used for shared libraries for the whole web server and WEB-INF/lib only for one web application.

Upvotes: 5

Related Questions