AWS networking

Java ClassLoader Mechanism Exaplained

Java code is compiled to .class files and JVM executes the byte codes in those .class files. That is how the java code is executed. By default java CLASSPATH is set to the current directory which is "." and JVM loads the classes in that Directory when executing.



When executing a java program, we have to notify the JVM telling where to find the .class files. ClassLoader is class in java that loads the classes. Classes can be loader from the system, network or from any other source.

There are three types of Class Loaders

Bootstrap ClassLoader : Bootstrap class loader loads class from rt.jar. rt.jar contains standard JDK class files. rt means "run time".  rt.java contains all the Java Runtime Libraries. Libraries like java.lang.String or java.io.File are loaded from this jar.  rt.jar should not be touched and JVM loads classes from rt.jar without any security checks that it does for other classes. Bootstrap class loader is the parent of all the other class loaders in java.

Bootstrap Class Loader is a part of the JVM Core. Bootstrap Class  Loader starts up and start the whole class loading process when the JVM starts. It is written in native language (C language). It loads classes such as java.util and the java.lang packages that contains the code that supports basic JRE environment.

Since Bootstrap class loader started with the JVM and not by a class loader, calling String.class.getClassLoader() will return a NULL. Bootstrap class loader doesn't have a parent and it is not loaded by a class loader.

Extension ClassLoader :  Extension ClassLoader loads class from jre/lib/ext directory or the directories mentioned in the java.ext.dirs property. Bootstrap ClassLoader is the parent of  Extension ClassLoader. jre/lib/ext is in the JRE


JAR files in the jre/lib/ext are treated as extensions by the JRE. Extension class loader is implemented by sun.misc.Launcher$ExtClassLoader

Application ClassLoader :  Application class loader is responsible for loading classes which are specific to the application. If we have written a java program in application.java and car.java files. In order to execute this program, Application class loader will have to load the application.class and car.class to the JVM.

JVM loads classes in the path which is set to CLASSPATH environmental variable. By default it is set to the current directory. Application class loader loads classes from one of these three places...
  • CLASSPATH

  • -classpath or -cp command line option

  • Class-Path attribute of Manifest file inside JAR
Extension ClassLoader is the parent of Application ClassLoader. Application ClassLoader  is implemented by sun.misc.Launcher$ExtClassLoader 


How Class Loader Works

There are three principles in java class loading

  1. Delegation Principal: If there is a class called myapp.class in our application and the first request to load the class will go to the Application class loader, then it will delegate it to its parent, which is Extension class loader. Then it will delegate the request again to its parent, which is Bootstrap class loader. Bootstrap class loader starts looking for myapp.class in the rt.jar  since it is not there, the request comes back to the Extension class loader. Now Extension class loader starts looking myapp.class in the /lib/ext. Since the class is also not it there the request goes to Application class loader. Now the Application class loader loads that class from the CLASSPATH. If the myapp.class was found during the checking in the Bootstrap or the Extension class loader, the class will be loaded at that time. The process of loading the class came all the way down to the Application class loader since the class was not found in any of the class loaders above the Application class loader.
                    

  2. Visibility Principal :  Child ClassLoader can see class loaded by Parent ClassLoader but vice-versa is not true. That means Extension class loader can't load the Application specific classes. Those classes are not visible to to the Extension class loader which is the parent of the Application class loader.

    Output is :



    It gives an error when trying to load VisibilityExample.class using Extension class loader

  3. Uniqueness Principle :  Classes loaded by Parent should not be loaded by Child ClassLoader again. That means classes loaded by Extension class loader should not be loaded by Application class loader.

It is possible to write code which violate the delegate and uniqueness principles but it is strongly advised to follow those rules when writing our own class loaders. Class.forName(classname) and Class.forName(classname, initialized, classloader) methods can be used to explicitly load classes in java. As i see, people who starts learning java should get to know about these concepts in the early stages of the learning curve. Even some developers who has worked with java for a long time doesn't have an idea about the class loading mechanism in java.

Comments