Tomcat Shared Class Loader

A classpath is an argument that tells the JVM where to find the classes and packages necessary to run a program. The classpath is always set from a source outside the program itself. Separating this responsibility from the program allows the Java code to reference the classes and packages in an abstract manner, allowing the program to be configured for use on any system.

In this article, I've shared my experience about Tomcat shared class loader.

What ?

Shared class loader is to share the classes and resources across All Web applications (unless Tomcat internal classes also need access, in which case you should put them in the Common class loader instead). And the objective of this shared class loader is to avoid duplicate resources, different version compatibility class loading issues and minimize the risk in version updates.

Where can we use it?

Now a days we're deploying multiple applications in Tomcat. When the time of server startup the classes, resources are loading from "WEB-INF/lib" directory and some of the libraries are duplicated in all the applications. In this use-case, we use shared class loader to avoid duplicate classes and class loading issues.

How to enable Shared Class Loader in Tomcat ?

Tomcat disables the shared class loader by default. In "conf/catalina.properties" file, we've option to define/enable shared class loader in "shared.loader" section.

Tomcat Shared Class Loader

Example location: shared.loader="${catalina.base}/lib/sharedlib","${catalina.base}/lib/sharedlib/*.jar"

After making this changes the shared.loader location ".jar" extension files are shared to all the applications (WARs).

Advantages:

      • Removing the duplicate version of JARs.
      • Avoiding junk memory creation issues.
      • Avoiding multiple version class loading compatibility issues.
      • Minimizing the JARs version up-gradation risks.