Core Java

Java ClassLoaders

Introduction:

In this tutorial, we’ll learn what is a classloader in Java. We’ll also look at various types of available Java Classloaders.

A Classloader in Java is responsible for loading the required classes in memory dynamically at runtime. The classes aren’t loaded in memory all at once but rather when really needed by the application.

Types of ClassLoaders:

Java has three types of classloaders available in its JRE:

1. Bootstrap ClassLoader:

Bootstrap classloader is responsible for loading the standard JDK class files from rt.jar and the ones located in $JAVA_HOME/jre/lib directory. It is also known as Primordial classloader.

The bootstrap or primordial classloader is the parent of all classloaders and the part of the core JVM library. Let’s try to print the classloader for our String class:

System.out.println(String.class.getClassLoader());

The above line of code will print null on the console. It simply means it has been loaded by our bootstrap classloader. Since the code for Java bootstrap classloader is written in the native language, it returns null.

2. Extension ClassLoader:

An extension classloader is responsible for loading extensions of the standard core Java classes. It basically loaded the classes from $JAVA_HOME/jre/lib/ext directory or any other directory pointed by java.ext.dirs system property.

The extension classloader in JVM is a subclass of bootstrap classloader. It is implemented by sun.misc.Launcher$ExtClassLoader class. We can check that out by printing the output of getClassLoader() on any of the extension classes.

3. System or Application ClassLoader:

The system or the application classloader loads all the application specific classes from our CLASSPATH environment variable, -classpath or -cp command line option. It is a child of our extension classloader and is implemented by sun.misc.Launcher$AppClassLoader.

So, suppose we have a class Person in our application. Let’s try to print its classloader:

System.out.println(Person.class.getClassLoader());

The above code will produce an output much like:

sun.misc.Launcher$AppClassLoader@4dadda7b

Also, we must know that all the classloaders in Java, except the bootstrap classloader, extend from java.lang.ClassLoader.

How Java Classes are Loaded?

Now that we know about types of Java classloaders, let’s look at how the classloaders work in Java:

 

Java Classloaders - How they work

Java Classloaders are hierarchical in nature. Whenever JVM requests to load a class, each classloader first delegates the request to its parent. If the parent fails to find the class to be loaded, then classloader itself tries to load the class using the java.net.URLClassLoader.findClass() method. If the last child classloader in the hierarchy also fails to find the class, it throws a java.lang.NoClassDefFoundError or a java.lang.ClassNotFoundException.

Java ClassLoader Principles:

There are three important rules or features of any Java classloader:

1. Delegation

Classloaders in Java always first delegate a class loading request to its parent classloader. In case the parent fails to find that class to be loaded, only then the child classloader attempts to load that class using the URLClassLoader.findClass() method.

Suppose that JVM requests to load an application-specific class – Person. The request will first move up the hierarchy until it reaches the bootstrap classloader. Since the bootstrap classloader fails to find this class, the request now moves downwards to the extension classloader. The extension classloader again fails to find Person class, so the request further moves down to the application/system classloader.

The application classloader will now load this class into the memory.

2. Visibility

Visibility principle for classloaders states that the child classloaders can see the classes loaded by its parent but vice-versa doesn’t hold true.

For instance, our Person class is loaded by application classloader. It means that the Bootstrap and Extension classloaders can’t see it. Also, let’s say we have another class A which is loaded by the extension classloader, then it’ll be visible to our application classloader but not to the bootstrap classloader.

3. Uniqueness

This rule states that a class loaded by the parent classloader should never be loaded by its child again.

Due to the delegation principle, it is quite evident that we’ll always have uniquely loaded classes. This is so because we always delegate upwards and a child attempts to load only when the parent fails.

We’ll ever hardly need to write our own custom classloader. Still, we must remember the above principles whenever attempting to do so.

Conclusion:

In this article, we looked at what are Java classloaders, their types and how they work. We can also write our own custom classloader by extending our class from the java.lang.ClassLoader.

Be the First to comment.

Leave a Comment

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