Class Loaders in Java | Class loader

 Class Loaders in Java 

  • Class loaders loads java classes dynamically into Java Virtual Machine (JVM).
  •  It is part of Java Runtime Environment (JRE).
  • There are 3 types of Class loaders.
 
 
Class loaders in java

1. Bootstrap class loader: 

It is responsible to load Core Java API classes.
i.e. the classes present in rt.jar 

 

Bootstrap class loader

 
  • Bootstrap class loader is responsible to load classes from Bootstrap classpath.
  • Bootstrap class loader is by default available with every JVM.
  • It is implemented in native language like C/ C + + and not implemented in Java.

2. Extension class loader: 

  • It is the child class of bootstrap class loader.

Extension class loader

  • Extension class loader is responsible to load classes from extension class path.
Extension class loader


  • Extension class loader is implemented in java and the corresponding .class file is
sun.misc.Launcher$ExtClassLoader.class


3. Application/ System class loader: 

  • It is the child class of extension class loader.
  • This class loader is responsible to load classes from application class path.
  • It internally used environment variable class path.
  • Application class loader is implemented in java and corresponding .class file name is
sun.misc.Launcher$AppClassLoader.class
Class loader image

How Class Loader Work


How class loader work in java

  • Class loader follow delegation hierarchy principle.
  • When ever JVM come a cross a particular class , first it will check whether the corresponding class file is already loaded or not.
  • If it is already loaded  in method area, then JVM will considered that loaded class.
  • If it is not loaded then JVM request to class loader sub system to load that particular class.
  • Then class loader sub system hand over request to Application class loader.
  • Application class loader delegates a request to Extension class loader which in turn delegates request to bootstrap class loader.
  • Bootstrap class loader will search in bootstrap class path. If it is available then corresponding .class will be loaded by Bootstrap class loader.
  • If it is not available then bootstrap class loader delegates the request to Extension class loader.
  • Extension class loader will search in extension class path. If it is available then it will be loaded, otherwise this class loader delegates the request to Application Class loader.
  • Application class loader will search in application class path. If it is available then it will be loaded, otherwise we will be get Runtime exception saying:
NoClassDefFoundError
or
ClassNotFoundException
Ex:
 
working of Class loader in java example

 

Note: 

Bootstrap class loader is not java object hence we got null in first case, but Extension and Application class loaders are java objects hence we are getting corresponding for the remaining 2 SOP.

 [ ClassName@hashcode in hexa decimal form ]

 

Class loader sub system will give the highest priority for Bootstrap class path and then Extension class path followed by Application class path.


Need of Customizer Class Loader

  • Default class loader will load .class file only once even though we are using multiples times in our program. 
  • After loading .class file if it is modified outside then default class loader would not load updated class files. (Because .class file already available in method area.)
  • We can resolve this problem by defining our own customizer class loader.
  • The main advantage of customizer class loader is, we can control class loading mechanism based on our requirement.
i.e. we can load .class file separately every time so that updated version available to our program.
 

Define Customizer class loader

We can define our own customizer class loader by extending java.lang.ClassLoader class.
 
Ex:
 
public class   CustClassLoader extends ClassLoader
{
   @Override
   public class loadClass(String nam) throws ClassNotFoundException
   {
       // check for update and load updated .class file and return corresponding class
   }
 
}
 
class Test
{
 
   public static void main(String[] agrs)
  {
          Dog d1 = new Dog();
          CustClassLoader cl = new CustClassLoader();
          cl.loadClass("Dog");
         .
         . 
        
        cl.loadClass("Dog");
         
  }
 
}

Note: 

  • While developing web servers and application servers, usually we can go for customizer class loader to customized class loading mechanism.
  • We can use java.lang.ClassLoader class to define our own customizer class loader.
  • Every class loader in java should be child class of java.lang.ClassLoader either directly or in-directly.
  •  Hence this class access base class for all custiomizer class loaders. 
 
RAKESH RAKA

I am Rakesh Raka, senior software engineer (JAVA) in Sopra Steria.

Post a Comment (0)
Previous Post Next Post