SoFunction
Updated on 2025-03-08

Use tomcat to set shared lib to share the same jar

As more and more projects are available, more and more release packages deployed on tomcat will be deployed, which will inevitably lead to many identical jars loading, occupying a large amount of memory in the permanent storage area. By setting the shared lib, the same jar will be controlled to load only one.

This has the following benefits:

1. Avoid loading the same jars in different projects and reduce memory usage in the permanent survival area.

2. Improve the startup speed of tomcat, because many duplicate jars are loaded less

1. How to set shared lib

Method 1:

Modify the file under the conf file and configure the path:

Configure the absolute path:

="D:hs/develop/shared/lib","D:/hs/develop/shared/lib/*.jar"

Then place the same jar in the specified folder.

Or configure the relative path:

="${}/shared/lib","${}/shared/lib/*.jar"

Then create a new shared directory under the same directory as conf and place the same jar in lib

Method 2:

Modify the file under the conf file, configure the path, and append the shared lib path:

="${}/lib","${}/lib/*.jar","${}/lib","${}/lib/*.jar","${}/lib/shared/*.jar"

Then create a new shared directory in the lib folder and place the same jar in the shared directory

2. The difference between

Under a tomcat, and point to the same location, that is, the parent directory of the directory such as bin.

If you install multiple Tomcat instances but do not want to install multiple software backups, you can use these two properties. In the tomcat directory, only the bin and lib directories are common to multiple tomcat examples. The other directories conf, logs, temp, webapps and work are their own independent backups for each Tomcat instance.

At this time they point to different locations:

(Installation directory): Point to the location of public information, which is the parent directory of bin and lib.

(Working Directory): Point to the location of private information for each Tomcat directory, which is the parent directory of conf, logs, temp, webapps and work.

3. Tomcat6 class loading mechanism

Commonclassloader

Responsible for loading all classes and jar packages in the $CATALINA_HOME/common directory. For detailed configuration, please refer to the configuration in the $CATALINA_HOME/conf/ file. The class loaded by this classloader is visible to the Server class loader and Webapp class loader; the Commonclass loader is created when Tomcat is started, and its parent classloader is the System class loader;

Server classloader

Responsible for loading Tomcat's core class, all classes and jars located in the $CATALINE_HOME/server directory can be specified by the configuration in it; it is created when Tomcat starts, and its parent loader is a Commonclass loader;

Sharedclass loader

Responsible for loading common classes on webapps, which can be specified by the user through attributes in the file; it is created when Tomcat is started, and its parentloader is also a Common class loader;

Webappclassloader

It is only responsible for loading classes under WEB-INF/classes and WEB-INF/lib in their respective apps; although its parentloader is a Shared class loader, its loading strategy is different from the default class loading mechanism;

The above is personal experience. I hope you can give you a reference and I hope you can support me more.