In-depth analysis and custom class loader system 24- Detailed platform-specific boot loader class

In-depth analysis systems and custom class loader Detailed platform-specific boot loader class

1. On the other root class loader and class loader:

Built into the JVM startup class loader will load java.lang.ClassLoader and other Java platform classes; when the JVM starts, a special machine code runs, it will load the extension class loader and class loader system; this special class of machine code called a boot loader (Bootstrap)

Start class loader is not Java class, while others are the Java class loader; boot loader is the class-specific machine instruction platform, which is responsible for opening the entire loading process.

All class loader (except the boot class loader) is implemented as a Java class. However, always have a first component to load a Java class loader, so that the entire loading process can proceed smoothly, load the first pure Java class loader is the class loader to start duties.

Start class loader will be responsible for loading the JRE for the basic components required for normal operation, which includes java.util and java.lang package classes, etc.

Therefore: ClassLoader (the system class loader) is a root class loader loaded!

 System.out.println(ClassLoader.class.getClassLoader());
 运行结果:
 	null

Therefore: extension class loader and a system class loader by the boot class loader are loaded (static inner class AppClassLoader ExtClassLoader and which are the Launcher, the class loader loads Launcher AppClassLoader is, the class loader ExtClassLoader )

 System.out.println(Launcher.class.getClassLoader());
 运行结果:
 	null

2.ClassLoader.getSystemClassLoader () source code interpretation:

​ Returns the system class loader for delegation. This is the default delegation parent for new ClassLoader instances, and is typically the class loader used to start the application.

Returns the system class loader for delegation. This is the default commissioned a new class loader instances of parents, usually the class loader used to start the application.

​ This method is first invoked early in the runtime’s startup sequence, at which point it creates the system class loader and sets it as the context class loader of the invoking Thread.

This method first run early in the startup sequence when calling, then it will create a system class loader and set the calling thread context class loader.

​ The default system class loader is an implementation-dependent instance of [this class].

The default system class loader is an example of class implementation-dependent.

​ If the system property “java.system.class.loader” is defined when this method is first invoked then the value of that property is taken to be the name of a class that will be returned as the system class loader. The class is loaded using the default system class loader and must define a public constructor that takes a single parameter of type ClassLoader which is used as the delegation parent. An instance is then created using this constructor with the default system class loader as the parameter. The resulting class loader is defined to be the system class loader.

① If you define a system property "java.system.class.loader" the first time this method is called, the value of the property will be treated as the name would return as the system class loader class . ** ② This class is the default class loader to load the system, and must define a public constructor, the constructor takes a delegate class parent class loader of the type used as a parameter. ** Then use this constructor creates an instance, and use the default class loader system as a parameter. Generating class loader is defined as a system class loader [class loader is to define a custom class loader for the system]. [Every sentence thief important! ! ! ]

​ If a security manager is present, and the invoker’s class loader is not null and the invoker’s class loader is not the same as or an ancestor of the system class loader, then this method invokes the security manager’s checkPermission method with a RuntimePermission(“getClassLoader”) permission to verify access to the system class loader. If not, a SecurityException will be thrown.

If there is a security manager and the caller's class loader is not empty, and the system class loader class loader is not the same caller or not the system class loader ancestors, this method uses RuntimePermission ( "getClassLoader" ) permission to call the security manager's checkPermission method to verify access to the system class loader. Otherwise, it will throw SecurityException.

Returns:
The system ClassLoader for delegation, or null if none

Returns: the delegation system class loader, or is none, returns empty.

3. Customize the system class loader: By "java.system.class.loader" set

  • MyTest16 custom class loader, which is provided to the system class loader, you need to define a public constructor, the constructor takes a parent class as the delegate class loader type in this parameter :( added in MyTest16.java Construction method)

    • public MyTest16(ClassLoader parent){
      	super(parent);
      }
      
    • In the absence of added constructor:

      //将MyTest16作为系统类加载器去执行MyTest23
       public static void main(String[] args) {
          System.out.println(System.getProperty("java.system.class.loader"));
       }
      ...\out\production\IdeaProject>java -Djava.system.class.loader=Jvm.MyTest16 Jvm.MyTest23
      
      执行结果:
         Error occurred during initialization of VM
         java.lang.Error: java.lang.NoSuchMethodException: Jvm.MyTest16.<init>(java.lang.ClassLoader)
                 at java.lang.ClassLoader.initSystemClassLoader(Unknown Source)
                 at java.lang.ClassLoader.getSystemClassLoader(Unknown Source)
         Caused by: java.lang.NoSuchMethodException: Jvm.MyTest16.<init>(java.lang.ClassLoader)
      
    • After adding the constructor:

      //将MyTest16作为系统类加载器去执行MyTest23
       public static void main(String[] args) {
          System.out.println(System.getProperty("java.system.class.loader"));
       }
      ...\out\production\IdeaProject>java -Djava.system.class.loader=Jvm.MyTest16 Jvm.MyTest23
      
      执行结果:
      		Jvm.MyTest16
      
  • Summary: can be known by java -Djava.system.class.loader = Jvm.MyTest16, MyTest16 explicitly specify the system class loader

4. Perform the following results in the console and the terminal:

public class MyTest23 {
    public static void main(String[] args) {
        System.out.println(System.getProperty("java.system.class.loader"));
        System.out.println(MyTest23.class.getClassLoader());
        System.out.println(MyTest16.class.getClassLoader());
        System.out.println(ClassLoader.getSystemClassLoader());//打印系统类加载器
    }
}
  • The results of the console ::

    null
    sun.misc.Launcher$AppClassLoader@18b4aac2
    sun.misc.Launcher$AppClassLoader@18b4aac2
    sun.misc.Launcher$AppClassLoader@18b4aac2  // app
    
  • Results of the terminal:

    Jvm.MyTest16
    sun.misc.Launcher$AppClassLoader@18b4aac2
    sun.misc.Launcher$AppClassLoader@18b4aac2
    [null]  //我这里不知道为什么是空,而老师讲的运行之后应该为:Jvm.MyTest16@....  //应该是MyTest16
    
Published 25 original articles · won praise 0 · Views 1438

Guess you like

Origin blog.csdn.net/qq_40574305/article/details/104793525