In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
How to deeply analyze the relevant knowledge of JVM ClassLoader, in view of this problem, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible method.
In-depth JVM: some knowledge about ClassLoader
one。 What is ClassLoader?
The Java program is not a native executable file, but consists of many independent class files, each of which corresponds to a Java class. In addition, these class files are not all loaded into memory immediately, but according to the needs of the program. ClassLoader is responsible for loading class files into memory.
II. The architecture of ClassLoader?
From the figure above, we can see the parent-child relationship between class loaders (note that it is not the set inheritance relationship of the class) and jurisdiction.
(1) BootStrap is the top-level class loader. It is written by C++ and has been embedded in JVM. It is mainly used to read JRE/lib/rt.jar, the core class library of Java.
(2) ExtensionClassLoader is an extended class library used to read Java and read JRE/lib/ext/*.jar.
(3) AppClassLoader is a class file used to read all jar packages or directories specified by CLASSPATH.
(4) CustomClassLoader is written by users and is used to read specified class files.
three。 What is the parental delegation model?
The model of parental delegation can be reflected through the following process:
(1) when "Class A Loader" loads a class, it first determines whether the class has already been loaded.
(2) if it has not been loaded, first entrust its "Class A loader" 's "parent class loader" to load the class, which is a process of continuous upward search, when all the "ancestor class loaders" (including bootstrapclassloader) of Class A have not been loaded into the class, then go back to the initiator "Class A loader" to load, if it cannot be loaded, throw ClassNotFoundException.
For more information, you can refer to java.lang.ClassLoader 's loadClass (Stringname,booleanresolve) method. The code is summarized as follows:
/ / First,checkiftheclasshasalreadybeenloaded Classc=findLoadedClass (name); if (c==null) {try {if (parentless null) {c=parent.loadClass (name,false);} else {c=findBootstrapClass0 (name);}} catch (ClassNotFoundExceptione) {/ / Ifstillnotfound,theninvokefindClassinorder / / tofindtheclass. The findClass method here should be overridden. By default, it throws ClassNotFoundException c=findClass (name) directly;}
So how do we verify this model? Let's take a look at the following program to find out ClassLoaderTest's classloader and all his ancestor loaders.
Packagecom.classloader.test; publicclassClassLoaderTest {publicstaticvoidmain (String [] args) {ClassLoaderloader=ClassLoaderTest.class.getClassLoader (); while (loaderloading null) {System.out.println (loader.getClass (). GetName ()); loaderloader=loader.getParent ();} System.out.println (loader);}}
The running results are as follows:
* the line result indicates that the classloader for ClassLoaderTest is AppClassLoader
The result of the second line indicates that the classloader for AppClassLoader is ExtClassLoder
The result of the third line says: null indicates that the classloader for ExtClassLoader is BootstrapClassLoader, and then we can pack the program into a jar package, ClassLoaderTest.jar, put it in the JRE\ lib\ ext\ directory, and then run the program again. The results are as follows:
Why are the results different?
Because the loading of the Java class satisfies the principle of parental delegation, when I load the ClassLoaderTest class, I first see whether the current class loader has already loaded this class. If not, then the classloader at the next level loads the class. If it can be loaded, it will automatically load all the jar packages under JRE\ lib\ ext. So when we put the ExtClassLoader under it, the program runtime uses the parent load class ExtClassLoader to load the class. You will find that com.classloader.test.ClassLoaderTest has been loaded, so currently this program only uses two class loaders, ExtClassLoader and BootStrap. You can see the runtime class loading process in more detail with java-verbose:classcom.classloader.test.ClassLoaderTest, as shown in the following figure:
This is the end of the answer to the question on how to deeply analyze the knowledge related to JVM ClassLoader. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel to learn more about it.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.