Use tomcat to set shared lib to share the same jar

  • 2021-11-02 03:37:30
  • OfStack

When there are more and more projects, there will be more and more distribution packages deployed in tomcat, so it is inevitable that many identical jar will be loaded, which takes up a lot of memory in the permanent memory area. Only one identical jar will be loaded by setting shared lib.

This has the following advantages:

1. Avoid loading the same jar for different projects and reduce the memory occupation of the permanent existence area

2. Improve the startup speed of tomcat, because many repetitive jar are loaded less

1. How to set shared lib

Mode 1:

Modify the catalina. properties file under the conf file to configure the path to shared. loader:

Configure Absolute Path:


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

Then put the same jar in the specified folder.

Or configure the relative path:


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

After that, create a new shared directory under the same directory as conf, and put the same jar in lib

Mode 2:

Modify the catalina. properties file under the conf file, configure the path of common. loader, and append the path of shared lib:


common.loader="${catalina.base}/lib","${catalina.base}/lib/*.jar","${catalina.home}/lib","${catalina.home}/lib/*.jar","${catalina.home}/lib/shared/*.jar"

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

2. Differences between catalina. home and catalina. base

Under one tomcat, catalina. home and catalina. base point to the same location, which is the parent directory of directories such as bin.

If you install multiple Tomcat instances and do not want to install multiple software backups, you can use these two attributes. Under the tomcat directory, only bin and lib directories are shared by multiple tomcat instances, and other directories conf, logs, temp, webapps and work are independent backups of each Tomcat instance.

At this time, they point to different positions:

catalina.home (Installation Directory): Points to the location of common information, which is the parent directory of bin and lib.

catalina.base (Working Directory): The location pointing to the private information of 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 under the directory of $CATALINA_HOME/common. For detailed configuration, please refer to common. loader configuration in the file of $CATALINA_HOME/conf/catalina. properties; The classloader loads classes that are visible to Server class loader and Webapp class loader; Commonclass loader is created when Tomcat is started, and its parent classloader is System class loader;

Server classloader :

The core class responsible for loading Tomcat, all classes in the $CATALINE_HOME/server directory, and jar, which can be specified by the server. loader configuration in catalina. propreties; It is created when Tomcat is started, and its parent loader is Commonclass loader;

Sharedclass loader :

Responsible for loading webapp common classes, which can be specified by users through shared. loader attributes in catalina. properties files; It is created when Tomcat is started, and its parentloader is also Common class loader;

Webappclassloader :

It is only responsible for loading the classes under WEB-INF/classes and WEB-INF/lib in the respective app; Although its parentloader is Shared class loader, its loading strategy and default class loading mechanism are not quite the same;


Related articles: