In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the relevant knowledge of "what is class loader ClassLoader". In the operation of actual cases, many people will encounter such a 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!
Class loader ClassLoader class loader working mechanism
The class loader is the component that looks for the section code file of the class and constructs the component that the class represents the object within the JVM. In Java, the class loader loads a class into JVM through the following steps:
[1.] Load: find and import Class files; [2.] Link: perform verification, preparation, and parsing steps, in which parsing steps are optional: validating: checking the correctness of the data loaded in the Class file; preparing: allocating storage space for static variables of the class; [2.3] parsing: transferring symbolic references to direct references; [3.] Initialization: initialize the static variables and static code blocks of the class.
Class loading is done by ClassLoader and its subclasses, and ClassLoader is an important Java runtime system component that is responsible for finding and loading Class bytecode files at run time. JVM produces three ClassLoader at run time: root loader, ExtClassLoader (extended class loader), and AppClassLoader (system class loader). Among them, the root loader is not a subclass of ClassLoader, it is written in C++, so we do not see it in Java, the root loader is responsible for loading the core class libraries of JRE, such as rt.jar, charsets.jar under the JRE target, and so on. Both ExtClassLoader and AppClassLoader are subclasses of ClassLoader. ExtClassLoader is responsible for loading the JAR class package in the JRE extension directory ext, and AppClassLoader is responsible for loading the class package under the Classpath path.
There is a parent-child relationship between these three class loaders, that is, the root loader is the parent loader of ExtClassLoader and ExtClassLoader is the parent loader of AppClassLoader. By default, using AppClassLoader to load the application's classes, we can do an experiment:
Public class ClassLoaderTest {public static void main (String [] args) {ClassLoader loader = Thread.currentThread (). GetContextClassLoader (); System.out.println ("current loader:" + loader); System.out.println ("parent loader:" + loader.getParent ()); System.out.println ("grandparent loader:" + loader.getParent (). GetParent ();}}
Run the above code, and the following message will be typed on the console:
The current loader:sun.misc.Launcher$AppClassLoader@131f71a parent loader:sun.misc.Launcher$ExtClassLoader@15601ea / / ① root loader cannot be accessed in Java, so it returns null grandparent loader:null
From the above output, we know that the current ClassLoader is AppClassLoader, the parent ClassLoader is ExtClassLoader, and grandfather ClassLoader is the root class loader, so only null is returned because its handle is not available in Java.
JVM loads classes using the "overall responsibility delegation mechanism", "overall responsibility" means that when a ClassLoader loads a class, unless another ClassLoader is explicitly used, the classes that this class depends on and references are also loaded by this ClassLoader; "delegation mechanism" refers to first entrusting the parent loader to find the target class, and only if it cannot find it before finding and loading the target class from its own classpath. From a security point of view, imagine what a terrible consequence it would be if someone wrote a malicious base class (such as java.lang.String) and loaded it into JVM. However, due to the "overall responsibility delegation mechanism", the java.lang.String is always loaded by the root loader, thus avoiding the above events.
ClassLoader important method
In Java, ClassLoader is an abstract class located in the java.lang package. Here are some important interface methods of this class:
The Class loadClass (String name) name parameter specifies the name of the class that the class loader needs to load, and must use a fully qualified class name, such as com.baobaotao. Beans.Car . This method has an overloaded method loadClass (String name, boolean resolve), and the resolve parameter tells the class loader whether or not the class needs to be parsed. Before initializing a class, you should consider the work of class parsing, but not all classes need to be parsed, and if JVM only needs to know whether the class exists or finds the superclass of the class, then it does not need to be parsed.
Class defineClass (String name, byte [] b, int off, int len) converts the byte array of class files into java.lang.Class objects inside JVM. Byte arrays can be obtained from the local file system and remote networks. Name is the fully qualified class name corresponding to the byte array.
Class findSystemClass (String name) loads the Class file from the local file system, and throws a ClassNotFoundException exception if the Class file does not exist on the local file system. This method is the default loading mechanism used by JVM.
Class findLoadedClass (String name) calls this method to see if ClassLoader has loaded a class. If loaded, the java.lang.Class object is returned, otherwise null is returned. If you forcibly load an existing class, a link error will be thrown.
ClassLoader getParent () gets the parent loader of the classloader. All classloaders except the root loader have one and only one parent loader. The parent loader of ExtClassLoader is the root loader. Because the root loader is not written by Java, it cannot be obtained and will return null.
In addition to the default three ClassLoader of JVM, you can write your own third-party class loader to meet some special requirements. After the class file is loaded and parsed, there will be a corresponding java.lang.Class class description object in JVM. All instances of this class will have a reference to the class description object, and the class description object will have a reference to the associated ClassLoader, as shown in the figure.
Each class has a corresponding java.lang.Class object in JVM, which provides a description of the class structure information. Arrays, enumerations, annotations, and basic Java types (such as int, double, and so on), and even void have corresponding Class objects. Class does not have a construction method for public. The Class object is automatically constructed by JVM by calling the defineClass () method in the class loader when the class is loaded.
Initialization of class
When will the class be initialized
Create an instance of the class, that is, new an object
Access the static variable of a class or interface, or assign a value to it
Call the static method of the class
Reflection (Class.forName ("com.lyj.load"))
Initialize a subclass of a class (the parent of the subclass is initialized first)
The startup class marked when JVM starts, that is, the class with the same file name and class name
Only in these 6 cases will the initialization of the class of the class be caused.
Initialization steps for the class:
If the class has not been loaded and linked, load and link first
If there is a direct parent class for this class and the class has not been initialized (note: in a class loader, the class can only be initialized once), initialize the direct parent class (not applicable to interfaces)
If there are initialization statements (such as static variables and static blocks) in the add class, execute these initialization statements in turn.
This is the end of the content of "what is the classloader ClassLoader". 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.
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.