What is ClassLoader in Java ?
- 4.6/5
- 4839
- Jul 20, 2024
The Java ClassLoader is a part of the Java Runtime Environment (JRE) that dynamically loads Java classes into the Java Virtual Machine (JVM) during runtime.
The JVM doesn't need to know about the underlying files or file systems in order to run Java programs thanks to class loaders.
Moreover, these Java classes aren't loaded into memory all at once, but rather when they're required by an application.
Not all classes are loaded by a single class loader, but there are three different class loaders to do the job.
1) BootStrap ClassLoader
When a JVM starts up, a special chunk of platform-specific machine code runs that loads the classloader subsystem and kicks off the whole classloading process.
This machine code is known as the Bootstrap/Primordial (or sometimes - Null) classloader.
It is not a Java class at all, as are all other classloaders.
Loading the first pure Java classloader is the job of the bootstrap classloader.
The bootstrap classloader also takes care of loading all of the code needed to support the basic Java Runtime Environment (JRE), including classes in the rt.jar, java.util and java.lang packages.
Bootstrap ClassLoader doesn't have any parent ClassLoaders.
2) Extension ClassLoader
The Extension ClassLoader is the child classloader of Bootstrap and the parent classloader of System classloader. It loads the extensions of core Java classes located inside $JAVA_HOME/jre/lib/ext directory or any other directory pointed to by the system property java.ext.dirs.
3) System ClassLoader
System ClassLoader is also called Application ClassLoader and it loads the classes found in the environment variable CLASSPATH, -classpath or -cp command line option. You can change the classpath using the "-cp" or "-classpath" switch.
How ClassLoader sub-system work in Java ?
Delegation
Class loaders follow the delegation model, where on request to find a class or resource, a ClassLoader instance will delegate the search of the class or resource to the parent class loader.
Let's say we have a request to load an application class into the JVM. The application class loader first delegates the loading of that class to its parent extension class loader, which in turn delegates it to the bootstrap class loader.
Only if the bootstrap and then the extension class loader are unsuccessful in loading the class, does the system class loader try to load the class itself.
If the last system class loader isn't able to load the class either, it throws java.lang.NoClassDefFoundError or java.lang.ClassNotFoundException.
Visibility
A class loaded by a parent ClassLoader is visible to the child ClassLoaders but a class loaded by a child ClassLoader is not visible to the parent ClassLoaders.
Uniqueness
As a consequence of the delegation model, it's easy to ensure unique classes, as we always try to delegate upwards.
If the parent class loader isn't able to find the class, only then will the current instance attempt to do so itself.
Custom ClassLoader
The built-in class loader is sufficient for most cases where the files are already in the file system.
However, in scenarios where we need to load classes out of the local hard drive or a network, we may need to make use of custom class loaders.