It is good to understand the Java class loading mechanism when doing Java development. The basic understanding of the class loading mechanism is also helpful for Java developers handling exceptions related to the ClassLoader (ClassLoader).
class Loader delegation mechanism
The loading of Java classes is done through the class loader (CL), which is responsible for loading classes into the JVM. Simple applications can use the Java platform's own class loader to load their own classes, while slightly more complex applications tend to customize class loading to load their own classes.
In Java, the ClassLoader is organized in a tree-like structure. Determines whether a class has been loaded by requesting a class loader and by looking in its cache. If the class is already in the cache, CL returns directly; otherwise, the ClassLoader delegates its parent classloader to load the class. If the ClassLoader does not have a parent class or the parent class cannot load the class, the ClassNotFoundException exception is thrown, at which point the requested ClassLoader attempts to find and load the class file that needs to be loaded in its own path. If the class is found, it is returned; otherwise it throws a ClassNotFoundException exception. Where the cache is looked up recursively from subclass to parent, until it reaches the root node of the tree (the class loader has a tree structure) or finds the desired class in the cache. If the cache search has reached the root node, the ClassLoader will again expand recursively from the parent class to the subclass to load the required classes. The overall order is as follows:
1. Caching
2. Parent Class
3. Sub- class
this mechanism ensures that class classes are loaded by the ClassLoader closest to the root node. Keep in mind that the parent class loader always has permission to load classes with precedence. This is important to ensure that the core Java classes are loaded by the Bootstrap class loader, which is used to ensure that the correct version of the class is loaded , such as Java.lang.Object. In addition, the Bootstrap class loader guarantees that a class load can only see classes loaded by itself or its parent class (or the parent class of the parent class, and so on), and not the classes loaded by its subclasses and sibling classes (classes that inherit the same parent class are called sibling classes).
Clarified the hierarchical relationship of class loaders. The Genga is a bootstrap class load that is implemented by the native method and cannot be instantiated in Java code.
After Bootstrap is the extension classloader, the main responsibility of the loader is to load the classes from the extended directory and provide simple access to the JVM extensions without modifying the user classpath in the new JVM. The system or application class loader is responsible for loading the class from the path specified in the environment variable classpath, which can be obtained by calling the Classloader.getsystemclassloader () method.
class Loading Phase
The specific class load can be divided into 3 phases: physical loading, linking, initialization.
1 during the physical loading phase, the requirement class can be found in the specified classpath path. If the class file can be found, the byte code is loaded by reading the class file. This process gives a basic memory structure for a single class object, but the concepts like methods, fields, and some references to other classes are not known at this stage.
2 the link stage can be split into 3 main stages, because this phase is more complex:
2.1 This procedure performs a series of checks on bytecode by adding a check byte code to the class.
2.2 class preparation. This phase prepares the necessary data structures to represent the fields, methods, and interfaces that are defined in the class.
2.3 resolves all other class references in a particular class. Classes can be applied in a number of ways, specifically as follows
2.3.1 Parent Class
2.3.2 Interface
2.3.3 field Type
2.3.4 method return value type
2.3.5 types of local variables used in methods
3 during the initialization phase, all the initialization blocks contained in the class are executed, and the static variables are initialized to the default values.
Interestingly, class loading can be implemented by a lazy-loading mechanism, so part of it is only loaded at the first use and not at the beginning.
Exception
The biggest challenge when dealing with class loading problems is that the problem is difficult to manifest in the class loading process, but The use of this class will only be reflected. The following are two related exceptions during class loading and possible causes for corresponding exceptions:
ClassNotFoundException
L when a loaded class-related file, directory, or other resource is not added to a class loader or parent ClassLoader
L When the class loader's parent class is set incorrectly
L When the class loader uses errors
Noclassdeffounderror
L when a loaded class-related file, directory, or other resource is not added to a class loader or parent ClassLoader
L When the class loader's parent class is set incorrectly
L When a class is loaded, the references contained in the class are not available to the class loader of the class, such as the ClassLoader subclass
1. This article is translated by the programmer architecture
2. This article is translated from http://www.techjava.de/topics/2008/01/java-class-loading/
3. Reprint Please be sure to indicate this article from : Programmer Architecture (No.:archleaner )
4. More articles please scan the code:
Java class loader in-depth parsing (ii)