Monday 10 June 2019

How class loading works?


There are three class loaders in Java.
a.   Application class loader loads the classes from application classpath.
b.   Extension class loader loads the classes from ext folder of jre.
c.    Bootstrap class loader loads the base classes (for example, classes from rt.jar file)


Whenever application wants to load a class, JVM delegate the call to application class loader to load the class. Application class loader delegates this call to Extension class loader. Extension class loader further delegates the call to  Bootstrap class loader.

If bootstrap class loader able to find the class, it loads the class, else it informs extension class loader to load the class. Extension class loader tries to load the class from JRE ext directory, if it able to find, then load the class, else it informs Application class loader to load the class. Application class loader tries to load the class from the folders that are available in class path. If application class loader finds the class, then it loads the class, else informs jvm that this class is not found to load. JVM throws NoClassDefFoundError.

App.java
package com.sample.app;

import java.net.URLClassLoader;

public class App {

 public static void main(String args[]) {

  URLClassLoader classLoader = (URLClassLoader) ClassLoader.getSystemClassLoader();

  do {
   System.out.println(classLoader);
   classLoader = (URLClassLoader) classLoader.getParent();
  } while (classLoader != null);
  
  System.out.println("Bootstrap class loader reached");

 }
}


Output
sun.misc.Launcher$AppClassLoader@2a139a55
sun.misc.Launcher$ExtClassLoader@7852e922
Bootstrap class loader reached

How to get the search paths of each class loader while loading the classes?
ClassLoader class provides 'getURLs' method, it return the search path of URLs for loading classes and resources.

URL[] urls = classLoader.getURLs();

App.java
package com.sample.app;

import java.net.URL;
import java.net.URLClassLoader;

public class App {

 public static void main(String args[]) {

  URLClassLoader classLoader = (URLClassLoader) ClassLoader.getSystemClassLoader();

  do {
   System.out.println(classLoader);
   URL[] urls = classLoader.getURLs();
   
   for(URL url : urls) {
    System.out.println(url);
   }
   
   classLoader = (URLClassLoader) classLoader.getParent();
   
  } while (classLoader != null);
  
  System.out.println("Bootstrap class loader reached");

 }
}


Sample Output

sun.misc.Launcher$AppClassLoader@2a139a55
file:/Users/krishna/Documents/EclipseWorkSpaces/Learnings/corsapp/build/classes/
file:/Users/krishna/Documents/Softwares/apache-tomcat-8.5.37/lib/el-api.jar
file:/Users/krishna/Documents/Softwares/apache-tomcat-8.5.37/lib/tomcat-i18n-es.jar
file:/Users/krishna/Documents/Softwares/apache-tomcat-8.5.37/lib/tomcat-websocket.jar
file:/Users/krishna/Documents/Softwares/apache-tomcat-8.5.37/lib/jasper.jar
file:/Users/krishna/Documents/Softwares/apache-tomcat-8.5.37/lib/jasper-el.jar
file:/Users/krishna/Documents/Softwares/apache-tomcat-8.5.37/lib/tomcat-util.jar
file:/Users/krishna/Documents/Softwares/apache-tomcat-8.5.37/lib/catalina-storeconfig.jar
file:/Users/krishna/Documents/Softwares/apache-tomcat-8.5.37/lib/jsp-api.jar
file:/Users/krishna/Documents/Softwares/apache-tomcat-8.5.37/lib/catalina-tribes.jar
file:/Users/krishna/Documents/Softwares/apache-tomcat-8.5.37/lib/catalina.jar
file:/Users/krishna/Documents/Softwares/apache-tomcat-8.5.37/lib/tomcat-jni.jar
file:/Users/krishna/Documents/Softwares/apache-tomcat-8.5.37/lib/websocket-api.jar
file:/Users/krishna/Documents/Softwares/apache-tomcat-8.5.37/lib/tomcat-coyote.jar
file:/Users/krishna/Documents/Softwares/apache-tomcat-8.5.37/lib/catalina-ha.jar
file:/Users/krishna/Documents/Softwares/apache-tomcat-8.5.37/lib/tomcat-api.jar
file:/Users/krishna/Documents/Softwares/apache-tomcat-8.5.37/lib/annotations-api.jar
file:/Users/krishna/Documents/Softwares/apache-tomcat-8.5.37/lib/jaspic-api.jar
file:/Users/krishna/Documents/Softwares/apache-tomcat-8.5.37/lib/catalina-ant.jar
file:/Users/krishna/Documents/Softwares/apache-tomcat-8.5.37/lib/ecj-4.6.3.jar
file:/Users/krishna/Documents/Softwares/apache-tomcat-8.5.37/lib/servlet-api.jar
file:/Users/krishna/Documents/Softwares/apache-tomcat-8.5.37/lib/tomcat-util-scan.jar
file:/Users/krishna/Documents/Softwares/apache-tomcat-8.5.37/lib/tomcat-i18n-ja.jar
file:/Users/krishna/Documents/Softwares/apache-tomcat-8.5.37/lib/tomcat-i18n-ru.jar
file:/Users/krishna/Documents/Softwares/apache-tomcat-8.5.37/lib/tomcat-jdbc.jar
file:/Users/krishna/Documents/Softwares/apache-tomcat-8.5.37/lib/tomcat-i18n-fr.jar
file:/Users/krishna/Documents/Softwares/apache-tomcat-8.5.37/lib/tomcat-dbcp.jar
sun.misc.Launcher$ExtClassLoader@7852e922
file:/Library/Java/JavaVirtualMachines/jdk1.8.0_191.jdk/Contents/Home/jre/lib/ext/sunec.jar
file:/Library/Java/JavaVirtualMachines/jdk1.8.0_191.jdk/Contents/Home/jre/lib/ext/nashorn.jar
file:/Library/Java/JavaVirtualMachines/jdk1.8.0_191.jdk/Contents/Home/jre/lib/ext/cldrdata.jar
file:/Library/Java/JavaVirtualMachines/jdk1.8.0_191.jdk/Contents/Home/jre/lib/ext/jfxrt.jar
file:/Library/Java/JavaVirtualMachines/jdk1.8.0_191.jdk/Contents/Home/jre/lib/ext/dnsns.jar
file:/Library/Java/JavaVirtualMachines/jdk1.8.0_191.jdk/Contents/Home/jre/lib/ext/localedata.jar
file:/Library/Java/JavaVirtualMachines/jdk1.8.0_191.jdk/Contents/Home/jre/lib/ext/sunjce_provider.jar
file:/Library/Java/JavaVirtualMachines/jdk1.8.0_191.jdk/Contents/Home/jre/lib/ext/sunpkcs11.jar
file:/Library/Java/JavaVirtualMachines/jdk1.8.0_191.jdk/Contents/Home/jre/lib/ext/jaccess.jar
file:/Library/Java/JavaVirtualMachines/jdk1.8.0_191.jdk/Contents/Home/jre/lib/ext/zipfs.jar
file:/System/Library/Java/Extensions/MRJToolkit.jar
Bootstrap class loader reached


Note
Extension and Application class loaders are written in Java, whereas Bootstrap class loader is written in native language like C.


Previous                                                 Next                                                 Home

No comments:

Post a Comment