Java class Loader

Source: Internet
Author: User

Java ClassLoader class Loader

The class loader is used to load a Java class into a virtual machine, and loading a class first requires a binary byte stream of that class, which can be achieved through a custom ClassLoader (overriding the Findclass method), allowing for a variety of flexible ways to obtain the class's binary byte stream.

Each classloader has a separate class namespace, and a class's uniqueness needs to be determined by its classloader to determine whether the two class is the same class if the two class is loaded by the same classloader, even if two classes come from the same class file, If they are loaded into a virtual machine by a different classloader, they will also be considered to be two different classes.

The class loader mainly includes the following types:

    • Launch class loader (Bootstrap ClassLoader)
      The startup ClassLoader is used to load the System class library in the Java_home\lib directory (for example: Rt.jar), which is part of the virtual machine and is implemented using C + +.
    • Extended class loading (Extension ClassLoader)
      The extension class loader is implemented by Sun.misc.launcher$extclassloader and is responsible for loading all class libraries in the Java_home\lib\ext.
    • Application class Loader (application ClassLoader)
      The application ClassLoader is implemented by Sun.misc.launcher$appclassloader and is responsible for loading all the class libraries contained in the CLASSPATH, which are the default ClassLoader used in the application, and if you do not use a custom class loader, Then all classes of the application are loaded by the ClassLoader.
Parental delegation Model

The relationships between the various classloader are as follows:

In the parental delegation model, in addition to starting the ClassLoader without a parent class loader, the other classloader should have a parent classloader (not necessarily implemented in an inherited manner, usually implemented in a composite way).

When a class loader loads a class, it first delegates its parent classloader to load, and only the parent loader fails to load itself. So all class load requests go through the Start class loader first.

The code implementation of the parent delegation model is in the Java.lang.ClassLoader LoadClass method:

protected synchronizedClass<?>LoadClass(String name,BooleanResolvethrowsclassnotfoundexception {//First, check if the class has already been loadedClass C =Findloadedclass(name);if(c = =NULL) {Try{if(Parent! =NULL) {c = parent.LoadClass(Name,false); }Else{c =Findbootstrapclassornull(name); }        }Catch(ClassNotFoundException e) {//ClassNotFoundException thrown if class not found          //From the Non-null parent class loader}if(c = =NULL) {//If still not found, then invoke Findclass in order        //To find the class.c =Findclass(name); }    }if(resolve) {Resolveclass(c); }returnC;}

(1) Determine if class has been loaded
(2) If it is not loaded, it is given to the parent ClassLoader to load, and if the parent ClassLoader is not specified (the parent classloader is null), the boot ClassLoader is loaded.
(3) After the above two steps are still unable to load, then call their own Findclass method to load

The parent delegation model ensures that the important system class is always loaded by the startup ClassLoader to ensure the stability and correctness of the Java operating environment.



Reference: "In-depth understanding of Java virtual machines"

Java class Loader

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.