ClassLoader and ClassLoading in Java



ClassLoader and ClassLoading in Java

Class Loader

Class Loader is a java class whose functionality is to Load the Class files, using the binary name, typically converts the name to file name and reads the class file.

Since class are loaded by different class loaders, every class's object have a reference to the ClassLoader, which loaded the class.

Some special points
  1. We can implement the subclasses of the ClassLoader to control the manner in which Java Virtual Machine loads the classes.
  2. ClassLoaders are much important for Security Managers, to indicate the Security domains.
  3. The three main principles used by ClassLoader are Delegation, Visibility and Uniqueness.
  4. Concurrent loading of class is referred as parallel capable class loaders. And they are supposed to register themselves by calling the ClassLoader.registerAsParallelCapable method. 
  5. JVM loads class platform-dependently.
  6. Classes which are not loaded from the normal file systems ( Such as, database or network), uses the defineClass(String name, byte[] b, int off, int len) method, which converts an array of bytes into an instance of that class.


public abstract class ClassLoader
extends Object

Binary names

Fully qualified name of the class is given to the ClassLoader as parameter, must also be a binary name.

 Valid samples :

   "java.lang.String"
   "javax.swing.JSpinner$DefaultEditor"
   "java.security.KeyStore$Builder$FileBuilder$1"
   "java.net.URLClassLoader$3$1"
 

Types of Class Loaders

There are three types of ClassLoaders, Bootstrap ClassLoader, Extension ClassLoader and the Application ClassLoaders. Also you can have custom ClassLoaders extending the actual ClassLoader class.


Bootstrap ClassLoader (Primordial ClassLoader)

Bootstrap ClassLoader's duty is to load standard java class files from rt.jar. It is the parent of all other ClassLoaders, and it doesn't have a parent ClassLoader.

rt.jar

Its a jar file which contains the API's of Java SE platform’s core.

Extension ClassLoader


When Extension ClassLoaders gets the request to load the class, it delegates the request to its parent, Bootstrap ClassLoader, and the Extension ClassLoader had the visibility to see if the class is loaded by its parent. If Bootstrap ClassLoader couldn't load it then,the Extension Class Loader, will try to load the class from the jre/lib/ext or the location mapped with java.ext.dirs.


Application ClassLoaders (System ClassLoaders)

The next one is the Application / System Classloader, Application ClassLoaders gets the initaial request to load class file. And it forwards the request the extension ClassLoader.
Application ClassLoader is used for loading the class files from classpath variable. It can be chanced using , -classpath or -cp command line option.


See the following example for better understanding

public class ClassLoading {

 public static void main(String[] args) {

  System.out.println("Class Loader for java.lang.String is : "
    + java.lang.String.class.getClassLoader());
  // for Bootstrap ClassLoader the class.getClassLoader() gives null
  // String class is part of the rt.jar

  System.out
    .println("Class loader for sun.net.spi.nameservice.dns.DNSNameService is : "
      + sun.net.spi.nameservice.dns.DNSNameService.class
        .getClassLoader());
  // DNSNameService is in the ext folder, so loaded by the ExtClassLoader

  System.out
    .println("Class loader for my com.demo.ClassLoading class(this class) is : "
      + ClassLoading.class.getClassLoader());
  // My application's class is loaded by the AppClassLoader

 }
}

Principles of Class Loading and How Loading is done?

The following are the 3 principles followed during a ClassLoading is done. These principles are explained based on the use case of loading a java clsss .

Delegation
When the Application / System Classloader, gets a request to load a class, it delegates the request to its parent, Extension ClassLoader, which again delegates to the Bootstrap ClassLoader.

Visibility
If the Bootstrap ClassLoader couldn't find and load the class, then the  Extension ClassLoader is able to see the classes loaded by the parent based on the principle of Visibility. The same happens, if the case of Extension ClassLoader fails to load a class, then the Application ClassLoader is able to view the classes loaded by its parent.


Uniqueness
Uniqueness is another principle followed, such that one class is loaded only once. That is a class loaded by the parent should not be loaded again by the child ClassLoaders.

But this can be done by writing custom ClassLoaders which does not follow these principles.





References



3) http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/6-b14/sun/misc/Launcher.java

No comments

Thanks for viewing the blog post. Follow the blog to receive the updates.. Powered by Blogger.