ClassLoader in Java

In this article we will understand the basics of Classloader in Java. Let us first understand what is the meaning of classloader.

What is the ClassLoader ?

As its name implies, ClassLoader is a class that loads other classes. More scientific: The Classloader loads the bytecodes in its class to memory, so it can be used throughout your application. The ClassLoader is in the java.lang.ClassLoader package. Understanding the classloader is important for working on the server development or any product developments which is more often design the custom classloaders to load the classes.

You would have come across the ClassNotFoundExecption many times while you are writing Java programming. Classloader is the culprit which causes this exception.

Why do we need the ClassLoader ?

The class loading mechanism allows us to have multiple classes with the same name (exactly the same) but different class loaders. This is what happens for example in web containers today, where we have several different applications but it can happen that one or two classes have the same name, this will not cause any conflict.

Let’s dive deeper into this concept. As we said earlier, everything in Java is loaded by class loaders specific (which will be explained later). So you may ask: If everything is loaded by a ClassLoader and ClassLoader itself is a class that must also be loaded, then who carries the first ClassLoader to load other classes? to solve this problem the “Bootstrap ClassLoader” which is written in native language, is loaded by JVM. It is responsible for initializing the packages essential to run the language pack as the “rt.jar”. Bootstarap classloader is a native implementation. So, it could be different for each JVM implementation.

So if the java classes are loaded dynamically (through the ClassLoader) can I replace the code of a class and load it at runtime? Yes you can. You can still load remote class through any URL at runtime.

Exemplifying the Java ClassLoader

Before jumping into the workings of each ClassLoader in native Java, we have to understand why its use has become so necessary.

The ClassLoader by default loads all classes that are present in your CLASSPATH, so in this case you do not even know the existence of this loading mechanism. Classes are identified by its fully qualified name + class loader that loaded it, so we are sure of a one Class Employee Application 001 is different from the class Employee Application 002.

The use of class loaders is useful, for instance, when working with various versions of applications with different libraries. Ex: Suppose, the application 001 can work with version 3.6 of Hibernate while the application 002 works with the version 3.3. If they were working in the same class loader then there would have been conflicts in classes as org.hibernate.Session will be loaded twice in memory.

Listing 1 : Class Loading

R = Class loadClass (String className, boolean resolveIt);

In the above code, the parameter className is the absolute name of the class, ie, the full name of the package path (net.javabeat.MyClass). The boolean parameter resolveIt says the classes associated with our loaded class, should also be loaded as if it were a cascade of loadings.

ClassLoader Engine

We have 3 types of ClassLoaders, they are:

Bootstrap ClassLoader

ClassLoader Extension

Application ClassLoader

Each of these have their respective function which will be explained in the following sections.

Bootstrap ClassLoader : This is responsible for loading classes from rt.jar and has no parent, this is the father of all others. Therefore, ensure that nobody will modify the rt.jar classes. For example: the package java.lang.

Extension ClassLoader : This is responsible for loading JARs that are within the property java.ext.dirs directory, by default this directory is: $ JAVA_HOME / lib / ext. Bootstrap ClassLoader is the parent classloader for this.

Application ClassLoader : This, is no less important and is responsible for loading all classes in your application, ie, sets your CLASSPATH. Extension ClassLoader is the parent classloader. For a complete example of loading a class dynamically through loadClass, see the listing below.

Summary

Creating your own ClassLoader is a very rare scenario, in fact you may never need to work with it directly. But understanding its usefulness as a whole is essential and knowledge of this is a difference for sure. You can solve problems like NoClassDefFoundError in a short time, just knowing the basic principle of how the ClassLoader works for example in scenarios where a class can exist physically, but in another ClassLoader that is not your application.

This article aims at helping you understand the classloader concepts even though you may not necessarily write your own classloader anytime.

About Manisha Patil

Manisha S Patil, currently residing at Pune India. She is currently working as freelance writer for websites. She had earlier worked at Caritor Bangalore, TCS Bangalore and Sungard Pune. She has 5 years of experience in Java/J2EE technologies.

Trackbacks

[…] This information includes the list of public as well as private members (methods) available for the class, the various public operations it supports etc. This section will guide you in making use of the […]