Class loader details

Classification of class loaders

  • The JVM supports two types of loaders, namely * * * Bootstrap Class Loader * * and * * * User-Defined Class Loader * *.
  • Conceptually, a custom class loader generally refers to a class loader defined by developers in a program, but the Java virtual machine specification does not define it, but * * * divides all class loaders derived from the abstract class ClassLoader into custom class loaders * * *.
  • No matter how the types of class loaders are divided, there are only three most common class loaders in the program, as shown below:
public class ClassLoaderTest {

    public static void main(String[] args) {

        //Get system class loader (application class loader)
        ClassLoader systemClassLoader = ClassLoader.getSystemClassLoader();
        System.out.println(systemClassLoader);  //sun.misc.Launcher$AppClassLoader@18b4aac2

        //Get its upper layer: extension class loader
        ClassLoader extClassLoader = systemClassLoader.getParent();
        System.out.println(extClassLoader); //sun.misc.Launcher$ExtClassLoader@1b6d3586

        //Get its upper layer:
        ClassLoader bootstrapClassLoader = extClassLoader.getParent();
        System.out.println(bootstrapClassLoader); //null

        //For user-defined classes, the system class loader is used by default
        ClassLoader classLoader = ClassLoaderTest.class.getClassLoader();
        System.out.println(classLoader);  //sun.misc.Launcher$AppClassLoader@18b4aac2

        //The String class is loaded using the bootstrap class loader. ---- > Java's core class libraries are loaded using the bootstrap class loader.
        ClassLoader classLoader1 = String.class.getClassLoader();
        System.out.println(classLoader1); //null

Classification of Loaders

Loader of virtual machine

Boot classloader (boot classloader, Bootstrap ClassLoader)

  • This class load is implemented in C/C + + language and nested inside the JVM.
  • It is used to load the core class library of Java (JAVA_HOME/jre/lib/rt.jar, resources.jar or the contents in sun.boot.class.path path) and provide classes required by the JVM itself
  • It does not inherit from java.lang.ClassLoader and has no parent class loader
  • Load extension classes and application class loaders and specify them as their parent class loaders.
  • For security reasons, the Bootstrap startup class loader only loads classes starting with package names java, javax, sun, etc

Extension ClassLoader

  • Written in Java language and implemented by sun.misc.Launcher$ExtLassLoader
  • Derived from ClassLoader class
  • The parent class loader is the boot class loader
  • Load the class library from the directory specified by the java.ext.dirs system attribute, or from the jre/lib/ext subdirectory (extension directory) of the JDK installation directory. If the JAR created by the user is placed in this directory, it will also be automatically loaded by the extension class loader

Application class loader (system class loader, AppClassLoader)

  • Written in java language and implemented by sun.misc.Launcher$AppClassLoader
  • Derived from ClassLoader class
  • The parent class loader is an extension class loader
  • It is responsible for loading the class library under the path specified by the environment variable classpath or the system attribute java.class.path
  • This class loader is the default class loader in the program. Generally speaking, the classes of Java applications are loaded by it
  • This class loader can be obtained through the ClassLoader#getSystemClassLoader() method
import sun.misc.Launcher;


public class ClassLoaderDemo {

    public static void main(String[] args) {
        System.out.println("***********************Start class loader***************************************");
        //Get bootstrap classloader
        URL[] urLs = Launcher.getBootstrapClassPath().getURLs();
        for (URL e : urLs) {

        //Select a class from the above path to see what its class loading is: boot class loader
        ClassLoader classLoader = Provider.class.getClassLoader();
        System.out.println(classLoader); //null

        System.out.println("***********************************extensions class loader ***********************************");
        String extDirs = System.getProperty("java.ext.dirs");
        for (String path : extDirs.split(";")) {
        //Select a class randomly from the above path to see what its class loader is
        ClassLoader classLoader1 = CurveDB.class.getClassLoader();
        System.out.println(classLoader1); //sun.misc.Launcher$ExtClassLoader@4b67cf4d

User defined class loader

  • In the daily application development of Java, class loading is almost performed by the above three kinds of loaders. When necessary, we can also customize the class loader to customize the loading method of the class.

Why customize the class loader?

  • Isolation loader
  • Modify how classes are loaded
  • Extended loader
  • Prevent source code leakage

Implementation steps of user-defined class loader:

  1. Developers can implement their own class loader by inheriting the abstract class java.lang.ClassLoader to meet some special needs
  2. Before JDK1.2, when customizing the class loader, you always inherited the ClassLoader class and overridden the loadClass() method to implement the custom class loader. However, after JDK1.2, you are no longer recommended to override the loadClass() method, but to write the custom class loading logic in the findClass () method
  3. When writing a custom class loader, if there are no too complex requirements, you can directly inherit the URLClassLoader class. In this way, you can avoid writing the findClass () method and its way to obtain the byte code stream, making the writing of the custom class loader more concise.

About ClassLoader

ClassLoader class, which is an abstract class. All subsequent class loaders inherit from ClassLoader (excluding startup class loader)

Ways to get ClassLoader

public class ClassLoaderTest1 {
    public static void main(String[] args) {
        try {
            ClassLoader classLoader1 = Class.forName("java.lang.String").getClassLoader();
            System.out.println(classLoader1); //null

            ClassLoader classLoader2 = Thread.currentThread().getContextClassLoader();
            System.out.println(classLoader2);  //sun.misc.Launcher$AppClassLoader@18b4aac2

            ClassLoader parent = ClassLoader.getSystemClassLoader().getParent();
            System.out.println(parent);  //sun.misc.Launcher$ExtClassLoader@1b6d3586

        } catch (ClassNotFoundException e) {

Tags: Java jvm

Posted on Wed, 10 Nov 2021 16:56:50 -0500 by WindomEarle