JVM load the class file principle

The concept of loading

The so-called load is to find a binary form of a class or interface and is configured with the binary form representative of the process of the class or object of this class interface.

Java class loader loads classes into the virtual machine

In Java, the class loader to load a class Java virtual machine, to be accomplished through three steps: loading and initializing the link, wherein the link can be divided into calibration and prepared, resolved.

  • Load: Find the class or interface and introduced into binary data;
  • Links: performing the following verification, preparation and analysis step, wherein the analysis step is optional;
    • Check: Check the validity of the class or interface to import binary data;
    • Preparation: Static variables are allocated to the class and initializes the storage space;
    • Analysis: The reference symbols converted into direct reference;
  • Initialization: Initialization Java code and static variables static Java code blocks activation of the class.

The JVM loads class

The JVM is loaded by a class and its subclasses ClassLoader achieved, Java ClassLoader is an important component of the Java runtime system. It is responsible for finding and class loading class files at run time.

Java applications to use the class loader

Application using a Java classloader two types: Root loader (on Bootstrap) and user-defined loader (user-defined).

  • Root loader in some way default class loading, including those of the Java API classes. During operation of a Java program can be installed class loader user-defined. Root loader intrinsic part of the virtual machine.
  • User-defined class loader is not inherent to the virtual machine, it is written in the Java language, after being compiled into class files are then loaded into the virtual machine, and the same as any other objects can be instantiated.

Architecture of the Java class loader is as follows:

        Bootstrap(根装载器)  
                |   
        Extension (扩展装载器)  
                |  
             System  
                |  
              UserDefine1
                 /   \  
        UserDefine2  UserDefine3  
                            |   
                      UserDefine4  

Java's class loading model

Java's class loading model is a proxy (Delegation) model. When the JVM class loader claim CL (ClassLoader) loading a class, CL this first class loader requests to his parent loader.

Only when the parent load is not loaded and can not load this class, CL was given the opportunity to load the class. Thus, all the class loader agency relationship constitutes a relationship of the tree. Root of the tree is the root class loader (bootstrap ClassLoader), it is expressed in the JVM to "null".

Except the root class loader loader has only one parent loader. When creating a loader, if not explicitly given parent loader, the system will default to the JVM loader loader as its parent.

Detailed description of various class loader

The following are described in detail for various class loader:

  1. Root (on Bootstrap) loader: the loading loader is no parent, it is part of the JVM implementations, when the operation load from sun.boot.class.path core code base.

  2. Extended (the Extension) loader: Inherited parent root loader loader, the loader may not root operating system is running about, the class loader is implemented in pure Java code, from which java.ext.dirs (extended directory) loaded code.

  3. System (System or Application) loader: loader loader for the expansion, we all know that when you install the JDK to set the environment variable (CLASSPATH), the class loader is loaded from java.class.path (CLASSPATH environment variable) code, which is also implemented in pure Java code, while the default or user-defined class loader parent loader.

  4. Applet (the Applet) loader: loader system loader, which specifies a particular directory on the network from the user to load the applet code.

Guess you like

Origin www.cnblogs.com/zxfei/p/11228880.html
Recommended