Showing posts with label Class Loaders. Show all posts
Showing posts with label Class Loaders. Show all posts

Tuesday, 31 May 2016

How does ClassLoader Work?

When JVM requests for a class, it invokes ClassLoader.loadClass() method by passing the fully classified name of the Class.


public Class<?> loadClass(String name) throwsClassNotFoundException;


loadClass() method calls for findLoadedClass() method to check that the class has been already loaded or not. It’s required to avoid loading the class multiple times.


protected final Class<?> findLoadedClass(String name);


If the Class is not already loaded then it will delegate the request to parent ClassLoader to load the class.

If the parent ClassLoader is not finding the Class then it will invoke findClass() method to look for the classes in the file system.


protectedClass<?> findClass(String name) throwsClassNotFoundException;

Java Classloader

The Java Classloader is a part of the JRE that dynamically loads Java classes into the JVM. Usually classes are only loaded on demand. The Java run time system does not need to know about files and file systems because of classloaders.





When the JVM is started, three class loaders are used:
Bootstrap class loader
Extensions class loader
System class loader

Bootstrap class loader
It loads the core Java libraries located in the <JAVA_HOME>/jre/lib directory. This class loader, which is part of the core JVM, is written in native code.

Extensions class loader
The extensions class loader loads the code in the extensions directories (<JAVA_HOME>/jre/lib/extor any other directory specified by the java.ext.dirs system property). It is implemented by the sun.misc.Launcher$ExtClassLoader class.

System class loader
It loads classes from the current classpath that can be set while invoking a program using -cp or -classpath command line options. This is implemented by the sun.misc.Launcher$AppClassLoader class.


Related Posts Plugin for WordPress, Blogger...