Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How the Java class is loaded in Tomcat

2025-04-03 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article introduces the relevant knowledge of "how Java classes are loaded in Tomcat". In the operation of actual cases, many people will encounter this dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

First, class loading

In JVM, all files are not loaded at once, but step by step, as needed.

For example, when JVM starts, different classes are loaded through different class loaders. When users need some additional classes in their own code, they load them into JVM through the loading mechanism and store them for a period of time to facilitate frequent use.

So what kind of loader to use and where to load classes are important knowledge in JVM.

Second, JVM class loading

The JVM class is loaded using the parent class delegation mechanism, as shown in the following figure:

Centralized class loaders are included in JVM:

BootStrapClassLoader Boot Class Loader

ExtClassLoader extended class loader

AppClassLoader application class loader

CustomClassLoader user-defined class loader

The differences between them are also explained above. It is important to note that different class loaders load different classes, so if user loader 1 loads a class, other users cannot use it.

When users need to load certain classes while JVM is running, they follow the following steps (parent class delegate mechanism):

The user's own classloader passes the load request to the parent loader, which then passes it to the parent loader all the way to the top of the loader tree.

The topmost class loader first loads for its specific location, and if it fails to load, it is transferred to the subclass.

If the underlying class load is not loaded, an exception ClassNotFoundException will be thrown.

Therefore, following this process, it is conceivable that if you also store the same class in the directory specified by CLASSPATH as in your own working directory, the files in the CLASSPATH directory will be loaded first.

Third, Tomcat class loading

Class loading is slightly different in Tomcat, as shown in the following figure:

When Tomcat starts, several kinds of loaders are created:

1. Bootstrap boot class loader

Load the classes required for JVM startup, as well as the standard extension classes (under jre/lib/ext)

2. System system class loader

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

3. Common generic class loader

Load some classes common to Tomcat usage 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 is created. This class loader loads the class file located in the jar file under WEB-INF/lib and the class file under WEB-INF/classes. Tomcat Server file configuration details! This article is recommended for everyone to read.

When an application needs to go to a class, the class is loaded in the following order:

Use bootstrap to boot the classloader to load

Load using the system system class loader

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

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

Use the common classloader to load in CATALINA_HOME/lib

That's all for "how Java classes are loaded in Tomcat". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report