The Java Classloader is a part of the Java Runtime Environment that dynamically loads Java classes into the Java Virtual Machine.
classes are loaded on demand in java run time environment. There is concept called delegation which is used in java class loading.
Compiled java classes (.class file) are packaged in JAR files in the Java language. A class with a given name can only be loaded once by a given classloader.
Each Java class must be loaded by a class loader. Furthermore, Java programs may make use of external libraries (that is, libraries written and provided by someone other than the author of the program) or they may be composed, at least in part, of a number of libraries.
There are three class loaders which are responsible for Classloadng inside JVM -
1) Bootstrap class loader:
2) Extensions class loader:
3) System class loader:
classes are loaded on demand in java run time environment. There is concept called delegation which is used in java class loading.
Compiled java classes (.class file) are packaged in JAR files in the Java language. A class with a given name can only be loaded once by a given classloader.
Each Java class must be loaded by a class loader. Furthermore, Java programs may make use of external libraries (that is, libraries written and provided by someone other than the author of the program) or they may be composed, at least in part, of a number of libraries.
There are three class loaders which are responsible for Classloadng inside JVM -
1) Bootstrap class loader:
- The bootstrap class loader loads the core Java libraries located in the
/jre/lib directory. It loads the core classes which is available in rt.jar file also called runtime jar. - It is a part of the core JVM, is written in native code.
2) Extensions class loader:
- The extensions class loader loads the code in the extensions directories
/jre/lib/ext or any other directory specified by the java.ext.dirs system property. - It is implemented in java by sun.misc.Launcher$ExtClassLoader class.
3) System class loader:
- The system class loader loads code found on java.class.path, which maps to the CLASSPATH environment variable.
- This is implemented by the sun.misc.Launcher$AppClassLoader class.
Classloading happens based on delegation concept. To understand refer the upper diagram. Application class loader delegates and ask extension class loader to load class and extension class loader delegates the same to bootstrap class loader.
If bootstrap class loader doesn't find the class and fails to load then the latter class loader tries to loads the required class.
Lets see the below code to understand how the class loading happens -
package com.refermynotes;
import java.net.URLClassLoader;
public class DelegationExample {
public static void main(String[] args) {
URLClassLoader classLoader = (URLClassLoader) ClassLoader.getSystemClassLoader();
do{
System.out.println(classLoader);
}while((classLoader = (URLClassLoader)classLoader.getParent()) != null);
System.out.println("Bootstrap classloader");
}
}
Output :
sun.misc.Launcher$AppClassLoader@73d16e93
sun.misc.Launcher$ExtClassLoader@15db9742
Bootstrap classloader

Comments
Post a Comment