Introduction to loading order of classes in Java (ClassLoader)

Source: Internet
Author: User

Transferred from: http://blog.csdn.net/eff666/article/details/52203406

1, Classnotfoundexcetpion
We are in the development, often can meet java.lang.ClassNotFoundExcetpion this anomaly, today I would like to summarize this question. For this exception, it essentially involves class loading in the Java technology architecture. Java's class loading mechanism is the core part of the technical system, although it does not deal with us directly, but the mechanism behind it has some understanding to help us to troubleshoot the application of class loading failure and other technical issues.
2. Class loading process
A Java file from being loaded into the unloaded life process, a total of 5 stages, the JVM divides the class loading process into:
   load-On (validate + prepare + Parse) initialization (pre-use preparation), use, uninstall
(1) Loading
The binary byte stream is obtained by the fully qualified name of a class, and the static storage structure represented by this byte stream is transformed into the runtime data structure of the method area. Finally, a class object representing this class is generated in the Java heap as the access entry for the data in the method area. In general, the binary data of the class is found and loaded.
(2) Links:
Validation: Ensure that the class being loaded is correct;
Prepare: Allocates memory for static variables of the class and initializes them to default values;
Parse: Converts a symbolic reference in a class to a direct reference;
(3) Assigning the correct initial value to the static variables of the class
3. Initialization of class
(1) When the class is initialized
1) Create an instance of the class, that is, the new object
2) Access a static variable of a class or interface, or assign a value to the static variable
3) Calling the static method of the class
4) Reflection (Class.forName ("Com.lyj.load"))
5) Initializes a subclass of a class (the parent class of the child class is initialized first)
6) Startup class indicated at the start of the JVM, which is the same class with the same file name and class name
(2) Sequence of initialization of classes
1) If this class has not been loaded and linked, then load and link it first
2) If the class has a direct parent class, and the class has not been initialized (note: In a classloader, the class can only be initialized once), then initialize the immediate parent class (not for the interface)
3) If there is an initialization statement (such as a static variable and a static block) in the join class, then the initialization statements are executed sequentially.
4) In general, the order of initialization is: (static variable, static initialization block) –> (variable, initialization block) –> constructor, if there is a parent class, the order is: Parent class static method –> Subclass static method –> Parent class construction Method-- Sub-class construction methods
4. Loading of classes
Class loading refers to reading the binary data in the class's. class file into memory, placing it in the method area of the run-time data area, and then creating a Java.lang.Class object of this class in the heap that encapsulates the class's objects in the method area class. Such as:

The end product of a class that is loaded is a class object that is located in the heap area. The class object encapsulates the data structure of the classes within the method area, and provides the Java programmer with an interface to access the data structures within the method area. There are several ways to load classes:
1) load directly from the local system
2) Download the. class file over the network
3) load. class files from archive files such as Zip,jar
4) Extract the. class file from the proprietary database
5) Dynamically compile Java source files into a. class file (server)
5. Loading device
The class loading of the JVM is done through ClassLoader and its subclasses, and the hierarchy and loading order of the classes can be described as follows:

(1) Loader introduction
1) Bootstrapclassloader (startup class loader)
Responsible for loading $JAVA_HOME中jre/lib/rt.jar all classes, loading the path or jar specified by System.getproperty ("Sun.boot.class.path").
2) extensionclassloader (Standard extension class loader)
Some jar packages that are responsible for loading the extensions in the JAVA platform, including the jar packages in Jre/lib/*.jar or-djava.ext.dirs specified directories in $java_home. The path or jar specified by System.getproperty ("Java.ext.dirs").
3) Appclassloader (System class loader)
Responsible for documenting the jar packages specified in the Classpath and the class in the directory
4) CustomClassLoader (custom loader)
ClassLoader, such as Tomcat and JBoss, which are customized by the application according to their own needs, will be implemented by the Java EE specification.

(2) Order of class loaders
1) The load process will first check whether the class has been loaded, check the order is bottom-up , from custom ClassLoader to bootstrap ClassLoader layer by level check, as long as a certain classloader has been loaded as if this class has been loaded, Ensure that only all classloader of this class are loaded once. The order of loading is top-down , that is, the upper layer tries to load the class one at a level.
2) When the class is loaded, each classloader will hand the load task to its parent, if the parent cannot find it, and then load it by itself.
3) Bootstrap Loader (boot class loader) is the top class loader, with its parent loader null.

Introduction to loading order of classes in Java (ClassLoader)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.