Loading is the process of finding the binary representation of a class or interface with a particular name and creating class or interface from the binary representation.
One of the important concepts before we can go into details is ClassLoaders. The Java ClassLoaders is a part of JRE that loads classes dynamically into the JVM. Basically they are responsible for locating libraries, reading their content and loading the classes contained within the libraries.
Below are three important types of ClassLoaders and the sequence in which they are invoked
1. Bootstrap ClassLoaders: This class loader is provided by the JVM and it loads the trusted classes that are located in jre\lib\rt.jar. Class in rt.jar provide core API functionality.
2. Extension ClassLoaders: rt.jar has a class called sun.misc.Launcher$ExtClassLoader which invokes the extension class loader. This class loaders loads the class in lib/ext directory. Classes loaded by extension class loader extends the core API functionality.
By packaging a class in a jar and saving the jar in lib/ext directory we can make the class load by Extension Class loader . One of the implication of making a class loaded by extension class loader is that we do not have to specify the classpath while executing the that class.
One of the important concepts before we can go into details is ClassLoaders. The Java ClassLoaders is a part of JRE that loads classes dynamically into the JVM. Basically they are responsible for locating libraries, reading their content and loading the classes contained within the libraries.
Below are three important types of ClassLoaders and the sequence in which they are invoked
1. Bootstrap ClassLoaders: This class loader is provided by the JVM and it loads the trusted classes that are located in jre\lib\rt.jar. Class in rt.jar provide core API functionality.
2. Extension ClassLoaders: rt.jar has a class called sun.misc.Launcher$ExtClassLoader which invokes the extension class loader. This class loaders loads the class in lib/ext directory. Classes loaded by extension class loader extends the core API functionality.
By packaging a class in a jar and saving the jar in lib/ext directory we can make the class load by Extension Class loader . One of the implication of making a class loaded by extension class loader is that we do not have to specify the classpath while executing the that class.
3. System ClassLoader: rt.jar has a class sun.misc.Launcher$AppClassLoader which invoke the System ClassLoader. This will load all the class in java.class.path which is specified in environment variable CLASSPATH.
A simple illustration.
A simple illustration.
1. Create a simple class.
public class StartMyApp {
public static void main(String[] args) {
System.out.println("in StartMyApp");
DependentApp dependentApp=new DependentApp();
dependentApp.myDependentMethod();
}
}
2. Another class
public class DependentApp {
public void myDependentMethod(){
System.out.println("In dependent methods...");
}
}
3. Run java StartMyApp -verbose:class command. You will get the below output on the console.
One important things to note is that any class is uniquely identified by (Name, Package, Class Loader).
Below figure illustrate what happen when we compile and run a java class from the perspective of loading the class.
Below figure illustrate what happen when we compile and run a java class from the perspective of loading the class.
Below figure illustrate the flow inside loadClass methods.
The above figure demonstrate delegation hierarchy in ClassLoaders.
Classloaders delegate finding classes and resources to their parent before searching their own classpath. If the parent class loader cannot find a class or resource, only then does the class loader attempt to find them locally.
This delegation model also ensure two things: Uniqueness and Visibility
A class is loaded only once which is uniqueness and second is that a child class loader can see class loaded by parent class loader but opposite is not true and this principle is visibility of class loader
Additional resource can be found here
http://www.onjava.com/pub/a/onjava/2005/01/26/classloading.html
https://docs.oracle.com/javase/specs/jvms/se7/html/jvms-5.html
http://javapapers.com/core-java/java-class-loader/
http://zeroturnaround.com/rebellabs/rebel-labs-tutorial-do-you-really-get-classloaders/2/
https://www.cis.upenn.edu/~bcpierce/courses/629/jdkdocs/api/java.lang.ClassLoader.html



No comments:
Post a Comment