【Introduction to Class Loader】
A classloader is used to load class files into a JVM virtual machine. There are three types of loaders in JVM, bootstrap class loader, extension class loader and application class loader. In addition, you can also inherit the java.lang.ClassLoader class to create a custom loader.
【Class loader type】
1. BootStrap: It is not a Java class. It is written in C++, embedded in the JVM kernel, and is used to call native code execution using JNI (JavaNativeInterface). The boot class loader is responsible for loading the core Java class library, such as the rt.jar file under JAVA_HOME/jre/lib. This jar file contains some common classes such as:
java.lang.System java.util.HashMap
2. ExtClassLoader: Inherited from the java.lang.ClassLoader class, used to load the jar file below the extension path, the extension path is JAVA_HOME/jre/lib/ext
3. Application class loader (AppClassLoader): inherits from the java.lang.ClassLoader class, used to load the class below classpath. laughing out loud
【Delegation mechanism of class loader】
1. When the application needs to load a class, the bytecode file of the required class will be loaded under the corresponding path in the order of BootStrap, ExtClassLoader, and AppClassLoader.
2. We can compile the custom class into a class file and type it into a jar package, and place it in the JAVA_HOME/jre/lib/ext path. When the class loader is loaded, it will be loaded from the JAVA_HOME/jre/lib/ext path using the ExtClassLoader loader, and will not be loaded under the classpath using the AppClassLoader loader.
3. After using the class loader in order of BootStrap, ExtClassLoader, AppClassLoader, and failing to load the class, the class will not be found: java.lang.ClassNotFoundException
【Application demo】
1.BootStrap: written in C++, embedded in the JVM kernel, not a Java class
ClassLoader loader = System.class.getClassLoader(); System.out.println(loader); //null
2. AppClassLoader: Use the AppClassLoader loader to load the class below classpath
ClassLoader loader = ClassLoaderTest.class.getClassLoader(); System.out.println(loader.getClass().getName()); //sun.misc.Launcher$AppClassLoader
3.ExtClassLoader: Used to load the Java class under the JAVA_HOME/jre/lib/ext path. Once found, it will not be loaded in the classpath.
Write a custom Hello class, type it into a jar package and export it to the C:/ProgramFiles/Java/jdk1.7.0/jre/lib/ext/excelsoft.jar path.
ClassLoader loader = Hello.class.getClassLoader(); System.out.println(loader.getClass().getName()); //sun.misc.Launcher$ExtClassLoader
==========================================
Class loaders are a relatively complex topic, just for the introduction!
Summarize
The above is all the content of this article about the basic analysis of jvm class loader, and I hope it will be helpful to everyone. Interested friends can continue to refer to other related topics on this site. If there are any shortcomings, please leave a message to point it out. Thank you friends for your support for this site!