In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains "how to use the latest version of JDK15's JVM class loader". The content of the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to use the latest version of JDK15's JVM class loader".
Type 1 loader
There is a power hierarchy similar to that of human society in the family of class loaders:
1.1 Bootstrap
The class loader, which is the highest level and is created when JVM starts, is usually implemented by native code related to os. It is the most basic class loader.
When JDK8
It should be noted that Bootstrap ClassLoader Wisdom loads a class library with a specific name, such as rt.jar. This means that our custom jar thrown into\ jre\ lib will not be loaded.
Responsible for loading the class libraries in the path specified by the / jre/lib or-Xbootclasspath parameter and recognized by the virtual machine into memory (identified by name, such as rt.jar, and not loading files that cannot be recognized), such as:
Object
System
String
Rt.jar and other jar packages for Java runtime
The jar package with a specific name in the directory specified by the system property sun.boot.class.path
When JVM starts, load rt.jar through Bootstrap ClassLoader and initialize sun.misc.Launcher to create instances of Extension ClassLoader and Application ClassLoader.
See which libraries are initialized by Bootstrap ClassLoader:
URL [] urLs = Launcher.getBootstrapClassPath () .getURLs (); for (URL urL: urLs) {System.out.println (urL.toExternalForm ());}
After JDK9
Responsible for loading the basic module classes at startup, such as:
Java.base
Java.management
Java.xml
1.2 Platform ClassLoader
Extension ClassLoader during JDK8
There is only one instance, implemented by sun.misc.Launcher$ExtClassLoader:
Responsible for loading
All class libraries in the path specified by\ lib\ ext or java.ext.dirs system variable
Load some extended system classes, such as XML, encryption, compression-related function classes, etc.
Replace with platform class loader when JDK9
Load some platform-related modules, such as java.scripting, java.compiler*, java.corba*.
Then why was the replacement abolished at 9: 00?
JDK8 mainly loads jre lib's ext, which is used when extending the jar package, which is not recommended, so it is abolished. JDK9 has modularity, and there is no need for this kind of extension loader.
1.3 Application ClassLoader
There is only one instance, implemented by sun.misc.Launcher$AppClassLoader.
When JDK8
Responsible for loading all class libraries in the directory specified by the system environment variable ClassPath or the system attribute java.class.path.
If you do not have your own loader defined in your application, the loader is the default classloader. The loader is available via java.lang.ClassLoader.getSystemClassLoader.
After JDK9
Application class loader, used to load application-level modules, such as:
Jdk.compiler
Jdk.jartool
Jdk.jshell
All class libraries in the classpath path
The second and third layer class loaders are implemented in Java language, and users can also
1.4 Custom Class Loader
The user-defined loader is a subclass of java.lang.ClassLoader, and you can customize how the class is loaded; however, the loading order of the custom class loader is at the end of all system class loaders.
1.5 Thread Context ClassLoader
Each thread has a class loader (introduced after jdk 1.2), which is called Thread Context ClassLoader. If the thread is not set when it is created, it inherits one from the parent thread by default. If it is not set in the application global, all Thread Context ClassLoader is Application ClassLoader. It can be set by Thread.currentThread (). SetContextClassLoader (ClassLoader) and obtained by Thread.currentThread (). GetContextClassLoader ().
What is the use of the thread context loader?
This class loader allows the parent class loader to load the required class libraries through the child class loader, which breaks the parent delegation model we'll talk about later.
What's the advantage of this?
With the thread context loader, we can achieve all the code hot substitution, hot deployment, as well as the hot update principle in Android.
2 verify class loader
2.1 View the local class loader
In a JDK8 environment, the execution results are as follows
The Parent of AppClassLoader is Bootstrap, which is implemented by Cplink +, and does not exist in the JVM system, so the output is null.
Characteristics of class loader
Class loaders do not need to wait for a class to be "actively used" for the first time. The JVM specification allows class loaders to preload a class when it anticipates that it will be used.
Java programs cannot directly refer to the startup class loader. If you directly set classLoader to null, the startup class loader will be used by default.
If the .class file is missing when loading, the LinkageError error will be notified when the class is used actively for the first time. If it has not been used, the error will not be reported.
If no parent loader is specified, the default is to start the loader
Each class loader has its own namespace, which consists of the classes loaded by the loader and all its parent loaders. Different namespaces can lead to the same full path name of the class.
The runtime package consists of classes of the same class loader, which determines whether two classes belong to the same runtime package, depending not only on whether the full path name is the same, but also on whether the definition class loader is the same. Only classes belonging to the same runtime package can be visible within each other's packages.
A low-level current class loader cannot overwrite classes that have been loaded by a higher-level class loader
If a low-level class loader wants to load an unknown class, ask politely, "excuse me, has this class been loaded?"
The high-level class loader asked will ask itself two questions.
Have I already loaded this class
If not, can this class be loaded
Only if all high-level class loaders answer "no" to both questions can the current class loader load this unknown class.
The green arrow on the left asks whether the class has been loaded up until Bootstrap ClassLoader, and then tries to see if it can be loaded. If neither of them can be loaded, it notifies the current class loader that initiated the load request to allow it.
In the three small tags on the right, the representative class libraries mainly loaded by this layer class loader are listed, in fact, more than that.
You can view all the loaded class libraries in Bootstrap with the following code
Execution result
The path loaded by Bootstrap can be appended, and it is not recommended to modify or delete the original loading path.
If the following startup parameter is added to JVM, the specified class can be read normally through Class.forName, indicating that this parameter can increase the class loading path of Bootstrap:
-Xbootclasspath/a:/Users/sss/book/ easyCoding/byJdk11/src
If you want to observe which classes in which jar package is loaded at startup, you can add
-XX:+TraceClassLoading
This parameter is very useful in resolving class conflicts. After all, different JVM environments are not consistent in the order in which classes are loaded.
Sometimes you want to observe the loading context of a specific class. Because of the large number of loaded classes, it is difficult to capture the loading process of the specified class during debugging, so you can use the conditional breakpoint function.
For example, to view the loading process of HashMap, make a breakpoint at loadClass and type as shown in the condition box
How does JVM establish the uniqueness of each class in JVM
The fully qualified name of the class and the ID of the class loader that loaded the class
After learning the implementation mechanism of the classloader, we know that the parent delegation model is not a mandatory model, and users can customize the classloader. Under what circumstances do we need to customize the classloader?
Isolated load class
In some frameworks, middleware is isolated from the application module and classes are loaded into different environments.
For example, a container framework in Ali ensures that the dependent jar package in the application does not affect the jar package used by the middleware runtime through a custom class loader
Modify the method of class loading
The loading model of the class is not mandatory, except for Bootstrap, other loading does not have to be introduced, or dynamically loaded on demand at a certain point in time according to the actual situation.
Extended loading source
Such as loading from databases, networks, and even TV set-top boxes.
Prevent source code from leaking
Java code is easily compiled and tampered with and can be compiled and encrypted. Then the class loader also needs to customize and restore the encrypted bytecode.
Steps to implement a custom class loader
Inherit ClassLoader
Override the findClass () method
Call the defineClass () method
The sample code for a simple class loader implementation is as follows
Because middleware generally has its own dependent jar package, it is often forced to arbitrate classes when referencing multiple frameworks in the same project. According to a certain rule, the version of the jar package is uniformly specified, resulting in some classes with the same package path and class name, which will cause class conflicts and lead to exceptions in the application.
The mainstream container class frameworks customize class loaders to achieve class isolation between different middleware and effectively avoid class conflicts.
Thank you for reading, the above is "how to use the latest version of JDK15 JVM class loader" content, after the study of this article, I believe you on how to use the latest version of JDK15 JVM class loader this problem has a deeper understanding, the specific use of the situation also needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
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.