Home  >  Article  >  Java  >  Introduction to several class loaders in Tomcat

Introduction to several class loaders in Tomcat

零下一度
零下一度Original
2017-07-24 09:26:512441browse

                                                                                                         

                 JVM class loaders: bootstrap class loader (bootstrap class loader), extension class loader (extension class loader), system class loader (also known as application class loader, system class loader). The bootstrap class loader is the parent of the extension class loader, which in turn is the parent of the system class loader.

(1) Bootstrap Classloader is implemented by JVM in C language using JNI (that is, it is not a subclass of Java.lang.ClassLoader). Responsible for loading the class library under %JAVA_HOME%\lib or the class specified by the -Xbootclasspath option or using the -D option to specify the sun.boot.class.path system property.

(2) Extension ClassLoader is implemented by sun.misc.Launcher$ExtClassLoader and is responsible for loading the class library in %JAVA_HOME%\jre\lib\ext or the class library specified by the system variable java.ext.dirs .

(3) System ClassLoader is implemented by sum.misc.Launcher$AppClassLoader and is responsible for loading classes involved in java applications. The loader can be obtained through ClassLoader.getSystemClassLoader()


When tomcat starts, several class loaders will be created:

1 Bootstrap boot class loader

Loads the classes required for JVM startup, as well as standard extension classes, located under jre/lib/ext.

2 System system class loader

Loads classes started by tomcat, such as bootstrap.jar, usually specified in catalina.bat or catalina.sh. Located under CATALINA_HOME/bin.

3 Common Universal Class Loader

Loads some classes common to tomcat and applications, located under CATALINA_HOME/lib, such as servlet-api.jar

4 webapp application class loader

After each application is deployed, a unique Class loader. The class loader will load the class in the jar file located under WEB-INF/lib and the class file under

WEB-INF/classes.

When the application needs a certain class, the class will be loaded in the following order

:

1 Use bootstrap to guide the class loader to load

2 Use the system class loader to load

3 Use the application class loader to load in WEB-INF/classes

4 Use the application class loader to load in WEB-INF/lib

5 Use the common class loader to load in CATALINA_HOME/lib

Another point to mention is that placing java files in the src folder in Eclipse will give priority to the classes in the jar package:

This is because the java files in the src folder in Eclipse and the JSP in webContent will be compiled into class files and placed in WEB-INF/class when tomcat starts. The jar package referenced externally by Eclipse is equivalent to being placed in WEB-INF/lib. Therefore, classes compiled from java files or JSP files must be loaded first

.

Also note that different versions of jar packages are placed in CATALINA_HOME/lib and WEB-INF/lib. This will lead to an error that the class cannot be loaded in some cases.

Also, if multiple applications use the same jar package file, and multiple copies are placed, it may cause class loading errors between multiple applications. ###

The above is the detailed content of Introduction to several class loaders in Tomcat. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn