Class loaders in java

Java ClassLoader

The Java ClassLoader is a part of the (JRE)Java Runtime Environment that dynamically loads Java classes into the (JVM)Java Virtual Machine at run time.

Java ClassLoader is an abstract class. It belongs to java.lang package. Java classes aren’t loaded into memory all at once, but when required by an application at that time, the Java ClassLoader is called by Java Runtime Environment, and these ClassLoaders load classes into memory dynamically.

 

Type of Classloaders

Java Classloader is mainly three types:

  • Bootstrap ClassLoader OR Primordial ClassLoader
  • Extension ClassLoader
  • Application ClassLoader OR System ClassLoader

 

BootStrap ClassLoader

This ClassLoader is Responsible to load classes from the jdk\jre\lib folder. All core java API classes present in rt.jar which is present in this location only. Hence all API classes (like String, StringBufferetc) will be loaded by Bootstrap class Loader only. It is a parent of all class loaders.

It doesn’t have any parents. Its job is to load the first pure Java ClassLoader. It is also called the Primordial ClassLoader.

Location:

 

 

 

 

 

 

This Location is Called BootstrapClassPath. That is BootstrapClassLoader is Responsible for Load Classes from BootstrapClassPath. BootstrapClassLoader is by Default Available with the JVM.

 

Extension ClassLoader

It is the Child of Bootstrap ClassLoader. It loads files from jre/lib/ext directory or any other directory pointed by the system property java.ext.dirs. ThisClassLoader is Responsible to Load Classes from Extension Class Path.

This ClassLoader is implemented in Java and the corresponding .class File Name is
sun.misc.Launcher$ExtClassLoader.class

Location:

System ClassLoader

It is the Child of Extension ClassLoader. This ClassLoader is Responsible to Load Classes from Application Class Path (Current Working Directory). It can be set while invoking the program using -cp or classpath command-line options.

It Internally Uses Environment Variable Class Path. Application ClassLoader is implemented in Java and the corresponding .class File Name issun.misc.Launcher$appClassLoader.class. An Application ClassLoader is also known as a System ClassLoader.

 

How Java ClassLoader Works?

ClassLoader follows Delegation Hierarchy Principle. Whenever JVM Come Across a Particular Class, first it will check whether the corresponding Class is Already Loaded OR Not. If it is Already Loaded in Method Area then JVM will Use that Loaded Class.

If it is Not Already Loaded then JVM Requests ClassLoaderSub System to Load that Particular Class.
Then ClassLoaderSub System Handovers the Request to Application ClassLoader. Application ClassLoader Delegates that Request to Extension ClassLoader and Extension ClassLoader in-turn Delegates that Request to Bootstrap ClassLoader. Bootstrap ClassLoader Searches in Bootstrap Class Path for the required .class File(jdk/jre/lib)

If the required .class is Available, then it will be Loaded. Otherwise, Bootstrap ClassLoader Delegates that Request to Extension ClassLoader.

Extension ClassLoader will Search in Extension Class Path (jdk/jre/lib/ext). If the required .class File is Available then it will be Loaded, Otherwise it Delegates that Request to Application ClassLoader. Application ClassLoader will Search in Application Class Path (Current Working Directory). If the specified .class is Already Available, then it will be Loaded. Otherwise, we will get Runtime Exception Saying ClassNotFoundException OR NoClassDefFoundError.

 

 

Read More topics related to the collection.

 

Read more topics related to java

 

Hope this was helpful for you. If you have any questions please feel free to leave a comment. Thank you for reading.

 

Leave a Reply

Your email address will not be published.