Java course of written report loaders are used to charge classes at runtime. ClassLoader inwards Java industrial plant on 3 principle: delegation, visibility and uniqueness. Delegation regulation frontward asking of course of written report loading to rear course of written report loader in addition to exclusively loads the class, if rear is non able to abide by or charge class. Visibility regulation allows kid course of written report loader to encounter all the classes loaded past times rear ClassLoader, only rear course of written report loader tin sack non encounter classes loaded past times child. Uniqueness regulation allows to charge a course of written report just once, which is basically achieved past times delegation in addition to ensures that kid ClassLoader doesn't reload the course of written report already loaded past times parent. Correct agreement of course of written report loader is must to resolve issues similar NoClassDefFoundError inwards Java in addition to java.lang.ClassNotFoundException, which are related to course of written report loading. ClassLoader is every bit good an of import topic inwards advanced Java Interviews, where skilful noesis of working of Java ClassLoader in addition to How classpath industrial plant inwards Java is expected from Java programmer. I accept e'er seen questions like, Can i course of written report hold upward loaded past times 2 dissimilar ClassLoader inwards Java on diverse Java Interviews. In this Java programming tutorial, we will larn what is ClassLoader inwards Java, How ClassLoader industrial plant inwards Java in addition to unopen to specifics nearly Java ClassLoader.
What is ClassLoader inwards Java
ClassLoader inwards Java is a course of written report which is used to charge class files inwards Java. Java code is compiled into course of written report file past times javac compiler in addition to JVM executes Java program, past times executing byte codes written inwards course of written report file. ClassLoader is responsible for loading course of written report files from file system, network or whatsoever other source. There are 3 default course of written report loader used inwards Java, Bootstrap , Extension in addition to System or Application course of written report loader.
Every course of written report loader has a predefined location, from where they loads course of written report files. Bootstrap ClassLoader is responsible for loading measure JDK course of written report files from rt.jar in addition to it is rear of all course of written report loaders inwards Java. Bootstrap course of written report loader don't accept whatsoever parents, if you lot telephone band String.class.getClassLoader() it volition render null and whatsoever code based on that may throw NullPointerException inwards Java. Bootstrap course of written report loader is every bit good known as Primordial ClassLoader inwards Java.
Extension ClassLoader delegates course of written report loading asking to its parent, Bootstrap and if unsuccessful, loads course of written report shape jre/lib/ext directory or whatsoever other directory pointed past times java.ext.dirs arrangement property. Extension ClassLoader inwards JVM is implemented by sun.misc.Launcher$ExtClassLoader.
Third default course of written report loader used past times JVM to charge Java classes is called System or Application course of written report loader in addition to it is responsible for loading application specific classes from CLASSPATH surround variable, -classpath or -cp ascendency occupation option, Class-Path attribute of Manifest file within JAR. Application course of written report loader is a kid of Extension ClassLoader in addition to its implemented past times sun.misc.Launcher$AppClassLoader class. Also, except Bootstrap course of written report loader, which is implemented inwards native linguistic communication to a greater extent than oft than non inwards C, all Java course of written report loaders are implemented using java.lang.ClassLoader.
Every course of written report loader has a predefined location, from where they loads course of written report files. Bootstrap ClassLoader is responsible for loading measure JDK course of written report files from rt.jar in addition to it is rear of all course of written report loaders inwards Java. Bootstrap course of written report loader don't accept whatsoever parents, if you lot telephone band String.class.getClassLoader() it volition render null and whatsoever code based on that may throw NullPointerException inwards Java. Bootstrap course of written report loader is every bit good known as Primordial ClassLoader inwards Java.
Extension ClassLoader delegates course of written report loading asking to its parent, Bootstrap and if unsuccessful, loads course of written report shape jre/lib/ext directory or whatsoever other directory pointed past times java.ext.dirs arrangement property. Extension ClassLoader inwards JVM is implemented by sun.misc.Launcher$ExtClassLoader.
Third default course of written report loader used past times JVM to charge Java classes is called System or Application course of written report loader in addition to it is responsible for loading application specific classes from CLASSPATH surround variable, -classpath or -cp ascendency occupation option, Class-Path attribute of Manifest file within JAR. Application course of written report loader is a kid of Extension ClassLoader in addition to its implemented past times sun.misc.Launcher$AppClassLoader class. Also, except Bootstrap course of written report loader, which is implemented inwards native linguistic communication to a greater extent than oft than non inwards C, all Java course of written report loaders are implemented using java.lang.ClassLoader.
In brusk hither is the location from which Bootstrap, Extension in addition to Application ClassLoader charge Class files.
1) Bootstrap ClassLoader - JRE/lib/rt.jar
2) Extension ClassLoader - JRE/lib/ext or whatsoever directory denoted past times java.ext.dirs
3) Application ClassLoader - CLASSPATH environment variable, -classpath or -cp option, Class-Path attribute of Manifest within JAR file.
How ClassLoader industrial plant inwards Java
As I explained before Java ClassLoader industrial plant inwards 3 principles : delegation, visibility and uniqueness. In this department nosotros volition encounter those rules inwards item in addition to empathise working of Java ClassLoader amongst example. By the agency hither is a diagram which explains How ClassLoader charge course of written report inwards Java using delegation.
Delegation principles
As discussed on when a course of written report is loaded in addition to initialized inwards Java, a course of written report is loaded inwards Java, when its needed. Suppose you lot accept an application specific course of written report called Abc.class, starting fourth dimension asking of loading this course of written report volition come upward to Application ClassLoader which volition delegate to its rear Extension ClassLoader which farther delegates to Primordial or Bootstrap class loader. Primordial volition aspect for that course of written report in rt.jar in addition to since that course of written report is non there, asking comes to Extension course of written report loader which looks on jre/lib/ext directory in addition to tries to locate this course of written report there, if course of written report is constitute at that spot than Extension course of written report loader volition charge that course of written report in addition to Application course of written report loader volition never charge that course of written report only if its non loaded past times extension class-loader than Application course of written report loader loads it from Classpath inwards Java. Remember Classpath is used to charge course of written report files spell PATH is used to locate executable similar javac or coffee command.
Visibility Principle
According to visibility principle, Child ClassLoader tin sack encounter course of written report loaded past times Parent ClassLoader only vice-versa is non true. Which hateful if course of written report Abc is loaded past times Application course of written report loader than trying to charge course of written report ABC explicitly using extension ClassLoader volition throw either java.lang.ClassNotFoundException. every bit shown inwards below Example
package test;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* Java programme to demonstrate How ClassLoader industrial plant inwards Java,
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* Java programme to demonstrate How ClassLoader industrial plant inwards Java,
* inwards especial nearly visibility regulation of ClassLoader.
*
* @author Javin Paul
*/
public class ClassLoaderTest {
public static void main(String args[]) {
try {
//printing ClassLoader of this class
System.out.println("ClassLoaderTest.getClass().getClassLoader() : "
+ ClassLoaderTest.class.getClassLoader());
//trying to explicitly charge this course of written report i time to a greater extent than using Extension course of written report loader
Class.forName("test.ClassLoaderTest", true
, ClassLoaderTest.class.getClassLoader().getParent());
} catch (ClassNotFoundException ex) {
Logger.getLogger(ClassLoaderTest.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
Output:
ClassLoaderTest.getClass().getClassLoader() : sun.misc.Launcher$AppClassLoader@601bb1
16/08/2012 2:43:48 AM test.ClassLoaderTest main
SEVERE: null
java.lang.ClassNotFoundException: test.ClassLoaderTest
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at sun.misc.Launcher$ExtClassLoader.findClass(Launcher.java:229)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:247)
at test.ClassLoaderTest.main(ClassLoaderTest.java:29)
* @author Javin Paul
*/
public class ClassLoaderTest {
public static void main(String args[]) {
try {
//printing ClassLoader of this class
System.out.println("ClassLoaderTest.getClass().getClassLoader() : "
+ ClassLoaderTest.class.getClassLoader());
//trying to explicitly charge this course of written report i time to a greater extent than using Extension course of written report loader
Class.forName("test.ClassLoaderTest", true
, ClassLoaderTest.class.getClassLoader().getParent());
} catch (ClassNotFoundException ex) {
Logger.getLogger(ClassLoaderTest.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
Output:
ClassLoaderTest.getClass().getClassLoader() : sun.misc.Launcher$AppClassLoader@601bb1
16/08/2012 2:43:48 AM test.ClassLoaderTest main
SEVERE: null
java.lang.ClassNotFoundException: test.ClassLoaderTest
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at sun.misc.Launcher$ExtClassLoader.findClass(Launcher.java:229)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:247)
at test.ClassLoaderTest.main(ClassLoaderTest.java:29)
Uniqueness Principle
According to this regulation a course of written report loaded past times Parent should non hold upward loaded past times Child ClassLoader again. Though its completely possible to write course of written report loader which violates Delegation in addition to Uniqueness principles in addition to loads course of written report past times itself, its non something which is beneficial. You should follow all class loader regulation spell writing your ain ClassLoader.
How to charge course of written report explicitly inwards Java
Java provides API to explicitly charge a course of written report past times Class.forName(classname) and Class.forName(classname, initialized, classloader), remember JDBC code which is used to charge JDBC drives nosotros accept seen inwards Java programme to Connect Oracle database. As shown inwards in a higher house representative you lot tin sack top advert of ClassLoader which should hold upward used to charge that especial course of written report along amongst binary advert of class. Class is loaded past times calling loadClass() method of java.lang.ClassLoader class which calls findClass() method to locate bytecodes for corresponding class. In this representative Extension ClassLoader uses java.net.URLClassLoader which search for course of written report files in addition to resources inwards JAR in addition to directories. whatsoever search path which is ended using "/" is considered directory. If findClass() does non constitute the course of written report than it throws java.lang.ClassNotFoundException in addition to if it finds it calls defineClass() to convert bytecodes into a .class representative which is returned to the caller.
Where to job ClassLoader inwards Java
ClassLoader inwards Java is a powerful concept in addition to used at many places. One of the popular representative of ClassLoader is AppletClassLoader which is used to charge course of written report past times Applet, since Applets are to a greater extent than oft than non loaded from cyberspace rather than local file system, By using dissever ClassLoader you lot tin sack every bit good loads same course of written report from multiple sources in addition to they volition hold upward treated every bit dissimilar course of written report inwards JVM. J2EE uses multiple course of written report loaders to charge course of written report from dissimilar location similar classes from WAR file volition hold upward loaded past times Web-app ClassLoader spell classes bundled inwards EJB-JAR is loaded past times unopen to other course of written report loader. Some spider web server every bit good supports hot deploy functionality which is implemented using ClassLoader. You tin sack every bit good job ClassLoader to charge classes from database or whatsoever other persistent store.
That's all nearly What is ClassLoader inwards Java in addition to How ClassLoader industrial plant inwards Java. We accept seen delegation, visibility in addition to uniqueness principles which is quite of import to debug or troubleshoot whatsoever ClassLoader related issues inwards Java. In summary noesis of How ClassLoader industrial plant inwards Java is must for whatsoever Java developer or architect to blueprint Java application in addition to packaging.
Further Learning
Java Memory Management
How HashMap industrial plant inwards Java