Monday, June 6, 2016

Class Loader in java

Applications written in statically compiled programming languages, such as C and C++, are compiled into native, machine-specific instructions and saved as an executable file. The process of combining the code into an executable native code is called linking - the merging of separately compiled code with shared library code to create an executable application. This is different in dynamically compiled programming languages such as Java. In Java, the .class files generated by the Java compiler remain as-is until loaded into the Java Virtual Machine (JVM) -- in other words, the linking process is performed by the JVM at runtime. Classes are loaded into the JVM on an 'as needed' basis. And when a loaded class depends on another class, then that class is loaded as well.
When a Java application is launched, the first class to run (or the entry point into the application) is the one with the public static void method called main(). This class usually has references to other classes, and all attempts to load the referenced classes are carried out by the class loader.
To get a feeling of this recursive class loading as well as the class loading idea in general, consider the following simple class:
public class ClassLoaderDemo {

     public static void main(String[] args) {
           System.out.println("Class Loaded successfully");
     }
}

C:\Users\admin\Desktop>java -verbose:class ClassLoaderDemo

[Loaded java.net.URLClassLoader$2 from C:\Program Files\Java\jre1.8.0_72\lib\rt.
jar]
[Loaded java.text.Format from C:\Program Files\Java\jre1.8.0_72\lib\rt.jar]
[Loaded java.text.MessageFormat from C:\Program Files\Java\jre1.8.0_72\lib\rt.ja
r]
[Loaded java.util.Locale$Category from C:\Program Files\Java\jre1.8.0_72\lib\rt.
jar]
[Loaded java.util.Locale$1 from C:\Program Files\Java\jre1.8.0_72\lib\rt.jar]
[Loaded java.text.FieldPosition from C:\Program Files\Java\jre1.8.0_72\lib\rt.ja
r]
[Loaded java.util.Date from C:\Program Files\Java\jre1.8.0_72\lib\rt.jar]
[Loaded java.text.AttributedCharacterIterator$Attribute from C:\Program Files\Ja
va\jre1.8.0_72\lib\rt.jar]
[Loaded java.text.Format$Field from C:\Program Files\Java\jre1.8.0_72\lib\rt.jar
]
[Loaded java.text.MessageFormat$Field from C:\Program Files\Java\jre1.8.0_72\lib
\rt.jar]
Error: Could not find or load main class ClassLoaderDemo
[Loaded java.lang.Shutdown from C:\Program Files\Java\jre1.8.0_72\lib\rt.jar]
[Loaded java.lang.Shutdown$Lock from C:\Program Files\Java\jre1.8.0_72\lib\rt.ja
r]

When application class is loaded, all other classes required by application class must be loaded by JVM as “on demand basis”
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/ext or 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.


Dynamic Class Loading
Loading a class dynamically is easy. All you need to do is to obtain a ClassLoader and call its loadClass()method. Here is an example:

public class MainClass {

       public static void main(String[] args) {

              ClassLoader classLoader = MainClass.class.getClassLoader();
              System.out.println("Class Loader is " + classLoader);

              try {
                     Class aClass = classLoader.loadClass("com.javadsalgo.MainClass");
                     System.out.println("MainClass.getName() = " + aClass.getName());
              } catch (ClassNotFoundException e) {
                     e.printStackTrace();
              }

       }
}

Output:

Class Loader is sun.misc.Launcher$AppClassLoader@18b4aac2
MainClass.getName() = com.javadsalgo.MainClass

No comments:

Post a Comment