Class Loader Delegation In Java With Example

Posted By : Neeraj Kumar | 29-May-2018

In Java, the class loader is included in the Java runtime which loads classes into Java Virtual Machine (JVM) on the basis of requirement and does not need to know about the directory structure or the filesystem to load from, thanks to Classpaths in java. For loading classes, java class loader supports Delegation Model. How let’s take a look.
 

In any java program, there are more than one class loaders.
1. The class loader that needed to load the application classes.

2. The class loader that needed to load the extension classes.

2. The class loader that needed to load the base classes.
 

There is a hierarchy of class loaders. At the very top, this hierarchy is the base class loader. And others come below it. As the base class loader is at the top of the hierarchy, all the class loaders other than the base class loader has a parent. The class loader may delegate to its parent, as this is the behavior inherited from the built-in class loaders.  When a class loader delegate to its parent that parent may or may not load the class as this parent does not have to load the class.
 

Now you might be wondering, wait a second what is going on, so let me break it down.

For loading classes there are following rules:

1. If a class loader loads a class once, it has to load the class in the future, typically a class loader loads a class once and then cache it for loading it in the future from the cache. This behavior comes from built-in class loaders.
 

If you load a very basic console application there will be three class loaders loading classes for your application:

1. Application class loader: this will load classes from CLASSPATH or -classpath or -cp or using MANIFEST file.

2. Extension class loader loads classes from extension library JRE/lib/ext.

3. Bootstrap class loader loads system classes from the runtime jar JRE/lib/rt.jar.
 

All class loaders other than the Bootstrap class loaders are written in Java itself, whereas, the Bootstrap class loader is written in C language.
 

Now let’s take a look the actual process of class loading with the help of an example which is a basic console java app and will print the class loader hierarchy. But first Let’s discuss what will happen when you will run the program defined below.

1. JVM asks the application class loader to load the classes. Application class loader will delegate to its parent in the delegation hierarchy the extension class loader.

2. Extension class loader will delegate to its parent Bootstrap class loader.

3. Now the Bootstrap class loader may load the classes from the java runtime (JRE/lib/rt.jar). Bootstrap loads the classes found and if bootstrap class loader fails to load some classes it will fail and pass on the class loader to its child class loader Extension class loader.

4. Now extension class loader will try to load the classes from the extension directory and it will load those classes found on the extension path and if it fails to load some classes it will pass on the class loader to its child in the delegation calls to the application class loader.

5. Now application class loader will try to load all remaining classes left unloaded by the parents, and if it loads all classes program runs successfully, however, on the other hand, it fails to load any remaining class the ClassDefNotFound Exception will be thrown and the program won’t run.
 

Class Loader Delegation in Java

Program:
 

package edu.nraj;

import java.net.URL;
import java.net.URLClassLoader;

public class ClassLoadersDelegationModel {
    public static void main(String[] args) {
        URLClassLoader classLoader = (URLClassLoader) ClassLoader.getSystemClassLoader();
        do {
            System.out.println(classLoader);
            for(URL url: classLoader.getURLs()) {
                System.out.printf("\t %s \n", url.getPath());
            }
        } while ((classLoader = (URLClassLoader) classLoader.getParent()) != null);
        System.out.println("BootStrap Class Loader");    
    }
}

 

Place it in directory structure of your choice like mine is:


javaTests/classes/src/edu/nraj/ClassLoadersDelegationModel.java
 

Now cd into the root of your project directory, like

$ cd javaTests
 

Now compile your code using javac compiler from the javaTests dir

$ javac -sourcepath src classes/src/edu/nraj/ClassLoadersDelegationModel.java
 

Now run the program

$ java -cp classes/src edu.nraj.ClassLoadersDelegationModel

 

Output will look similar to
 

sun.misc.Launcher$AppClassLoader@73d16e93

     /home/neeraj/javaTests/classes/src/

sun.misc.Launcher$ExtClassLoader@33909752

     /usr/local/java/jdk1.8.0_144/jre/lib/ext/sunec.jar

     /usr/local/java/jdk1.8.0_144/jre/lib/ext/jfxrt.jar

     /usr/local/java/jdk1.8.0_144/jre/lib/ext/localedata.jar

     /usr/local/java/jdk1.8.0_144/jre/lib/ext/nashorn.jar

     /usr/local/java/jdk1.8.0_144/jre/lib/ext/cldrdata.jar

     /usr/local/java/jdk1.8.0_144/jre/lib/ext/sunjce_provider.jar

     /usr/local/java/jdk1.8.0_144/jre/lib/ext/zipfs.jar

     /usr/local/java/jdk1.8.0_144/jre/lib/ext/dnsns.jar

     /usr/local/java/jdk1.8.0_144/jre/lib/ext/jaccess.jar

     /usr/local/java/jdk1.8.0_144/jre/lib/ext/sunpkcs11.jar

BootStrap Class Loader

In the program:

1. Grab a reference to system class loader and cast it to URLClasssLoader.

2. Now we will print the class loader parent in a loop until it returns null.

3. When getParent() does return null this means we have now reached the top of the  hierarchy which is Bootstrap class loader, so print it.

4. We are also printing the URL these class loaders trying to load classes from.

About Author

Author Image
Neeraj Kumar

Neeraj is a JAVA Developer who possesses skill set in: Data Structures, Core Java, Java Enterprise Edition (Servlets, JSP, Standard Java Beans), Spring (Spring-core, spring-MVC, spring-Data-Access, Spring-Data-JPA), Hibernate, JPA, HTML, CSS, JavaScri

Request for Proposal

Name is required

Comment is required

Sending message..