In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article shows you how to understand Java's ClassLoader, which is concise and easy to understand, and can definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.
Explain the ClassLoader of Java.
I'm sure you don't use much about ClassLoader, but you may be asked a lot during the interview. So I've sorted out some related uses about it here.
ClassLoader is a class loader provided by Java, which is used to load Java classes into the Java virtual machine.
The Java program (class file) is not a local executable. When you run the Java program, you first run the JVM (Java Virtual Machine), and then load the Java class into the JVM to run. The part responsible for loading the Java class is called Class Loader.
JVM itself contains a ClassLoader called BootstrapClassLoader, and like JVM, BootstrapClassLoader is implemented in native code and is responsible for loading the core JavaClass (that is, all classes at the beginning of java.*). In addition, JVM provides two ClassLoader, both of which are written in Java and loaded by BootstrapClassLoader; Extension ClassLoader is responsible for loading extended Javaclass (such as all classes at the beginning of javax.* and classes stored in JRE's ext directory), and ApplicationClassLoader is responsible for loading the application's own classes.
When running a program, JVM starts, runs bootstrapclassloader, the ClassLoader loads the java core API (ExtClassLoader and AppClassLoader are also loaded at this time), then calls ExtClassLoader to load the extended API, and finally AppClassLoader loads the Class defined in the CLASSPATH directory, which is the most basic loading process of a program.
After understanding the above process, we will use the specific code to see how the class is loaded.
one
two
three
four
five
six
seven
eight
nine
Package com.neo.classloader
Public class ClassLoaderTest {
Public static void main (String [] args) {
ClassLoader loader = Thread.currentThread () .getContextClassLoader ()
System.out.println (loader)
System.out.println (loader.getParent ())
System.out.println (loader.getParent () .getParent ())
}
}
After running, output the result:
one
two
three
Sun.misc.Launcher$AppClassLoader@64fef26a
Sun.misc.Launcher$ExtClassLoader@1ddd40f3
Null
As you can see from the above results, the parent Loader of ExtClassLoader is not obtained, because the Bootstrap Loader (bootstrap classloader) is implemented in C, and there is no definite way to return the parent Loader, so null is returned. The hierarchical relationship of these kinds of loaders is shown in the following figure:
Note: here the parent class loader is not implemented through inheritance relationships, but by composition.
From the perspective of the Java virtual machine, there are only two different kinds of classloaders: the startup classloader: it is implemented using C++ (here only Hotspot, the default virtual machine after JDK1.5, there are many other virtual machines implemented in Java), which is part of the virtual machine itself. All other class loaders: these class loaders are implemented in the Java language, independent of the virtual machine, and all inherit from the abstract class java.lang.ClassLoader. These class loaders need to be loaded into memory by the startup class loader before loading other classes.
From the perspective of Java developers, classloaders can be roughly divided into the following three categories:
Boot class loader: Bootstrap ClassLoader, which is responsible for loading class libraries stored in JDK\ jre\ lib (JDK represents the installation directory of JDK, the same below), or in the path specified by the-Xbootclasspath parameter, and can be recognized by the virtual machine (such as rt.jar, all java. The first class is loaded by Bootstrap ClassLoader). The startup class loader cannot be referenced directly by the Java program.
Extension class loader: Extension ClassLoader, which is implemented by sun.misc.Launcher$ExtClassLoader and is responsible for loading all class libraries (such as javax. Ext) in the JDK\ jre\ lib\ ext directory or in the path specified by the java.ext.dirs system variable. The developer can use the extension class loader directly.
Application class loader: Application ClassLoader, this class loader is implemented by sun.misc.Launcher$AppClassLoader, which is responsible for loading the class specified by the user classpath (ClassPath). Developers can use this class loader directly. If the application has not customized its own class loader, this is generally the default class loader in the program.
Applications are loaded by these three types of loaders, and we can add custom class loaders if necessary. Because JVM's native ClassLoader only knows how to load standard java class files from the local file system, if you write your own ClassLoader, you can do the following:
1. Automatically verify the digital signature before executing the unbelievable code.
2. Dynamically create customized build classes that meet the specific needs of users.
3. Get java class from specific places, such as database and network.
JVM class loading mechanism
Overall responsibility, when a class loader is responsible for loading a Class, other Class that the Class depends on and references will also be loaded by the class loader, unless it is shown to use another class loader to load
Parent class delegate, first let the parent class loader try to load the class, and try to load the class from its own classpath only if the parent class loader cannot load the class
Caching mechanism, caching mechanism will ensure that all loaded Class will be cached. When a certain Class is needed in the program, the class loader first looks for the Class from the cache. Only if the cache does not exist, the system will read the corresponding binary data of this class, convert it into Class objects, and store it in the cache. This is why after you modify Class, you must restart JVM before the changes to the program will take effect.
Class is loaded in three ways:
1. When the command line starts the application, JVM initializes and loads it.
2. Dynamic loading through Class.forName () method
3. Dynamic loading through ClassLoader.loadClass () method
Example:
one
two
three
four
five
six
seven
eight
nine
ten
eleven
twelve
thirteen
fourteen
Package com.xttblog.classloader
Public class loaderTest {
Public static void main (String [] args) throws ClassNotFoundException {
ClassLoader loader = HelloWorld.class.getClassLoader ()
System.out.println (loader)
Loader.loadClass ("Test2")
}
}
Test2.java
one
two
three
four
five
Public class Test2 {
Static {
System.out.println ("static initialization block executed!")
}
}
If you switch the loading mode separately, you will have different output results.
The difference between Class.forName () and ClassLoader.loadClass ()
Class.forName (): loads the .class file of the class into jvm, interprets the class, and executes the static block in the class
ClassLoader.loadClass (): only one thing to do is to load the .class file into the jvm, the contents of the static will not be executed, and the static block will be executed only in the newInstance.
Class.forName (name, initialize, loader) takes a parameter function that also controls whether static blocks are loaded. And only the newInstance () method is called to create the object of the class by calling the constructor.
Summary: the loading mechanism and ClassLoader of the Java class may seem complex, but they are actually very simple. You can better understand it by reading the source code or by running the examples.
The above is how to understand the ClassLoader of Java. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.
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.