Java Class Loaders and Internal Working

Have you ever come across errors and issues like NoClassDefFoundError in Java and java.lang.ClassNotFoundException . Java Class Loader is the Culprit for those issues. So , In Order to fix and understand why those issues are occurring, you need to have a strong command of Java Class Loaders .The Java ClassLoader is a part of the JRE responsible for loading classess dynamically in JVM .

Must Read Posts-

  1. Whats are  Custom ClassLoaders In Java and How it Works?

What is a ClassLoader in Java:-

In Java you must be aware of the command java HelloWorldit starts your application or piece of code. The above command tells us that there is a binary file named HelloWorld.class and run that class. In order to run that binary code, we have to load that binary piece of code in memory. This loading of class in memory or to JVM is done by ClassLoader

A Java code that we write is not a single executable file like in C, C++  but instead, it is composed of many individual class files, each of which corresponds to a single Java class.

These class files are not loaded in memory at once, these are rather loaded on demand as needed, Now are you wondering who loads those classes?  Well, the answer is Class-Loader!

ClassLoaders are the part of JVM that loads classes into memory. ClassLoaders in java are responsible for loading class files from file, network or any other source.

Types Of Class Loaders :-

1) Bootstrap ClassLoader – JRE/lib/rt.jar

2) Extension ClassLoader – JRE/lib/ext or any directory denoted by java.ext.dirs

3) Application ClassLoader – ClassPath environment variable, -classpath or -cp option, Class-Path attribute of Manifest inside JAR file.

Some Points to Remember about the ClassLoaders :-

  1. A class is loaded only once into the JVM.
  2. Every class loader has some specific location from where it picks up the classes to load.
  3. The bootstrap class loader loads JDK internal classes, typically loads rt.jar and other core classes, for example, java.lang.* package classes.
  4. Bootstrap  ClassLoader is the parent of all class loaders.
  5. Extension ClassLoader delegates class loading request to its parent loader( Bootstrap) and if unsuccessful, loads class form JRE/lib/ext directory or any other directory pointed by  java.ext.dirs system property.
  6. Third default class loader used by JVM to load Java classes is called System or Application class loader and it is responsible for loading application related classes from Classpath variable.

Internal Working Of Java Class Loaders:-

Class loading is guided by three simple principles:

  1. Delegation – The Request to load a class is delegated to its immediate Parent until it reaches bootstrap ClassLoader
  2. Visibility –  Classes Loaded by Parent is Visible to all its child, but vice versa is not valid.The parent can’t see the classes loaded by its child.
  3. Uniqueness – A class will be loaded only once in whole Class Loading life cycle.

java class loaders