Thursday 1 December 2016

what is class loader and its execution life cycle ?

Class loader is part of JRE, which is responsible to load all the required classes dynamically in memory , it helps JVM initialize and execute the java application without knowing the files located in the system. With the help of class loader, JVM execute the byte code of .class files which is compiled from .java files by java compiler (javac).

In java, there are 3 types or hierarchy in the class loader.
  1. Bootstrap loader, parent class of all loader.
  2. Extension loader, extends bootstrap loader.
  3. System or Application loader.

1) Bootstrap class loader

Parent class of all loader and core part of JVM, which loads the java core libraries or classes located from <JAVA_HOME>/jre/lib/rt.jar.It is written in native code. 

2) Extension loader

Extension class loader extends bootstrap loader , which loads the java classes from the <JAVA_HOME>/jre/ext directory or any other directory specified by java.ext.dirs system property. Extension loader is implemented by the sun.misc.Launcher$ExtClassLoader class.

3) Sytem or Application class loader

System class loader extends Extension loader and loads all the classes from java.class.path which  is CLASSPATH environment variable,-classpath or -cp option, Class-Path attribute of Manifest inside JAR file.  System loader is implemented by the sun.misc.Launcher$AppClassLoader class.


Life cycle of class loader






When JVM is requesting the object of Employee.class with the help of System class loader, it delegates control to its super class Extension loader and in turn its delegates to its super class Bootstrap class loader. Bootstrap try to looks from <JAVA_HOME>/jre/lib. if present it returns the Employee class definition otherwise, Extension loader take care of the control to load from <JAVA_HOME>/jre/ext directory or any other directory specified by java.ext.dirs system property.if present, it returns the Employee class definition, otherwise its subclass System loader looks from CLASSPATH. If class is not present in any of the class loader, then application throws ClassNotFoundException, otherwise it loads the Employee.class in the memory , which helps JVM to execute the application.  The entire life cycle works in the concept of delegation principle.


That's all about what is class loader and its execution life cycle.




If you like the above solution . Please share this blog






No comments:

Post a Comment

s