Class Loaders in Java

Class loaders are responsible for loading Java classes during runtime dynamically to the JVM. They are part of JRE. JVM doesn’t need to know about the underlying files or file systems in order to run Java programs thanks to class loaders.

Also, these Java classes aren’t loaded into memory all at once, but when required by an application. This is where class loaders come on stage, they are responsible for loading classes into memory.

Types of built-in class loaders :

  • Application (or system) class loader : loads application files located in the classpath
  • Extension class loader : loads classes that are an extension of standard core Java classes
  • Bootstrap (or primordial) class loader : parent of all others. Note that bootstrap class loader is written in native code, not Java – so it doesn’t show up as Java class. Hence, the behavior of bootstrap class loader will differ across JVMs

Java classes are loaded by an instance of the class java.lang.ClassLoader. However, class loaders are classes themselves. So, there is a question : who loads the java.lang.ClassLoader itself ? The answer is – it is a Bootstrap class loader. Bootstrap class loader is mainly responsible for loading JDK internal classes, typically rt.jar and other core libraries located in $JAVA_HOME/jre/lib directory. Bootstrap class loader is part of the JVM, written in native code, loaded by the JVM and serves as a parent of all other ClassLoader instances.

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 these classes are available to all applications running on the platform. Extension class loader loads classes from the JDK extension directory, usually $JAVA_HOME/jre/lib/ext directory or any other directory mentioned in the java.ext.dirs system property.

Application (or system) 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. Application class loader is a child of Extension class loader.

public void printClassLoaders() throws ClassNotFoundException {
 
    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());
}

The code snippet above will print out the following :

Class loader of this class:sun.misc.Launcher$AppClassLoader@18b4aac2
Class loader of Logging:sun.misc.Launcher$ExtClassLoader@3caeaf62
Class loader of ArrayList:null
 
The Application class loader loads the class where the example method is contained. Application (or system) class loader loads files from the classpath. Extension class loader loads the Logging class. It means that Logging class was found in $JAVA_HOME/jre/lib/ext directory or any other directory mentioned in the java.ext.dirs system property. The Bootstrap class loader loads ArrayList class. ArrayList was found in one of core libraries in $JAVA_HOME/jre/lib directory. Note that ArrayList displays null in the output. This is because the Bootstrap class loader is written in native code, not Java – so it doesn’t show up as Java class. Due to this reason, the behavior of the bootstrap class loader will differ across JVMs.

Next

Leave a Reply

Your email address will not be published. Required fields are marked *