美文网首页
JAVA Classloader

JAVA Classloader

作者: lessIsMore_技术笔记 | 来源:发表于2021-01-22 14:19 被阅读0次

    Types of Built-in Class Loaders

    Let's start by learning how different classes are loaded using various class loaders using a simple example:

    publicvoidprintClassLoaders()throwsClassNotFoundException{    System.out.println("Classloader of this class:"+ PrintClassLoader.class.getClassLoader());    System.out.println("Classloader of Logging:"        + Logging.class.getClassLoader());    System.out.println("Classloader of ArrayList:"        + ArrayList.class.getClassLoader());}

    When executed the above method prints:

    Class loader of this class:sun.misc.Launcher$AppClassLoader@18b4aac2

    Class loader of Logging:sun.misc.Launcher$ExtClassLoader@3caeaf62

    Class loader of ArrayList:null

    2.1. Bootstrap Class Loader

    Java classes are loaded by an instance of java.lang.ClassLoader. However, class loaders are classes themselves. Hence, the question is, who loads the java.lang.ClassLoader itself?

    This is where the bootstrap or primordial class loader comes into the picture.

    It's mainly responsible for loading JDK internal classes, typically rt.jar and other core libraries located in $JAVA_HOME/jre/lib directory. Additionally, Bootstrap class loader serves as a parent of all the other ClassLoader instances.

    This bootstrap class loader is part of the core JVM and is written in native code as pointed out in the above example. Different platforms might have different implementations of this particular class loader.

    2.2. Extension Class Loader

    The extension class loader is a child of the bootstrap class loader and takes care of loading the extensions of the standard core Java classes so that it's available to all applications running on the platform.

    Extension class loader loads from the JDK extensions directory, usually $JAVA_HOME/lib/ext directory or any other directory mentioned in the java.ext.dirs system property.

    2.3. System Class Loader

    The system or application class loader, on the other hand, takes care of loading all the application level classes into the JVM. It loads files found in the classpath environment variable, -classpath or -cp command line option. Also, it's a child of Extensions classloader.

    相关文章

      网友评论

          本文标题:JAVA Classloader

          本文链接:https://www.haomeiwen.com/subject/qpiszktx.html