Class Loader

In this tutorial explain to

What is Class Loader?

Types of Class Loader.

Principle of Class Loader.

Class Loader hierarchy.


     ClassLoader 

The class loader concept, one of the cornerstones of the Java virtual machine, describes the behavior of converting a named class into the bits responsible for implementing that class. Because class loaders exist, the Java run time does not need to know anything about files and file systems when running Java programs.



The Java ClassLoader is a part of the Java Runtime Environment that dynamically loads Java classes into the JavaVirtual Machine. The Java run time system does not need to know about files and file systems because of classloaders.

Java classes aren’t loaded into memory all at once, but when required by an application. At this point, the Java ClassLoader is called by the JRE and these ClassLoaders load classes into memory dynamically.


Types of ClassLoaders in Java

Not all classes are loaded by a single ClassLoader. Depending on the type of class and the path of class, the ClassLoader that loads that particular class is decided. To know the ClassLoader that loads a class the getClassLoader()method is used. All classes are loaded based on their names and if any of these classes are not found then it returns a NoClassDefFoundError or ClassNotFoundException.

  1. BootStrap ClassLoader: A Bootstrap Classloader is a Machine code which kickstarts the operation when the JVM calls it. It is not a java class. Its job is to load the first pure Java ClassLoader. Bootstrap ClassLoader loads classes from the location rt.jar. Bootstrap ClassLoader doesn’t have any parent ClassLoaders. It is also called as the Primodial ClassLoader.

 

  1. Extension ClassLoader: The Extension ClassLoader is a child of Bootstrap ClassLoader and loads the extensions of core java classes from the respective JDK Extension library. It loads files from jre/lib/ext directory or any other directory pointed by the system property java.ext.dirs.

 

  1. System ClassLoader: An Application ClassLoader is also known as a System ClassLoader. It loads the Application type classes found in the environment variable CLASSPATH, -classpath or -cp command line option. The Application ClassLoader is a child class of Extension ClassLoader.

Note: The ClassLoader Delegation Hierarchy Model always functions in the order Application ClassLoader->Extension ClassLoader->Bootstrap ClassLoader. The Bootstrap ClassLoader is always given the higher priority, next is Extension ClassLoader and then Application ClassLoader.


ClassLoader in Java works on three principle:

  • Delegation
  • Visibility
  • Uniqueness

The Delegation principle forward request of class loading to parent class loader and only loads the class, if parent is not able to find or load class. 

Visibility principle allows child class loader to see all the classes loaded by parent ClassLoader, but parent class loader cannot see classes loaded by child. 

Uniqueness principle allows to load a class exactly once, which is basically achieved by delegation and ensures that child ClassLoader doesn't reload the class already loaded by parent.


How do Class Loaders Works?

 Class loaders are part of the Java Runtime Environment. When the JVM requests a class, the class loader tries to locate the class and load the class definition into the runtime using the fully qualified class name.

The java.lang.ClassLoader.loadClass() method is responsible for loading the class definition into runtime. It tries to load the class based on a fully qualified name.

If the class isn't already loaded, it delegates the request to the parent class loader. This process happens recursively.

Eventually, if the parent class loader doesn’t find the class, then the child class will call java.net.URLClassLoader.findClass() method to look for classes in the file system itself.

If the last child class loader isn't able to load the class either, it throws java.lang.NoClassDefFoundError or java.lang.ClassNotFoundException.


Classloader hierarchy

Whenever a new JVM is started the bootstrap classloader is responsible to load key Java classes (from java.lang package) and other runtime classes to the memory first. The bootstrap classloader is a parent of all other classloaders. Consequently, it is the only one without a parent. Second phase comes the extension classloader. It has the bootstrap classloader as parent and is responsible for loading classes from all .jar files kept in the java.ext.dirs path–these are available regardless of the Java Virtual Machine's classpath. The third and most important classloader from a developer's perspective is the system classpath classloader, which is an immediate child of the extension classloader. It loads classes from directories and jar files specified by the CLASSPATH environment variable, java.class.path system property or -classpath command line option.

Most Java programmers will never need to explicitly use class loaders (except to load resources so that it still works when they're bundled in JARs), let alone write their own. ClassLoaders are used in very large systems and server applications to do things like:

  • Modularize a system and load, unload and update modules at runtime
  • Use different versions of an API library (e.g. an XML parser) in parallel
  • Isolate different applications running within the same JVM (ensuring they don't interfere with each other, e.g. through static variables)

Class Loaders are a functional component of Java Virtual Machine, which loads class data from the .class file or from over the network in to the Method Area in Heap . Each class loader has it's own name space and classes invoked by a particular class loader gets into it's name space. Classes invoked by two different class loaders will not have visibility over each other, resulting in enhanced security. Class loader parent child delegation mechanism ensures java api classes could never be hacked by unauthorized code. This is because class loaders exist, the Java run time does not need to know anything about files and file systems when running Java programs. Moreover, Java ClassLoader is written in the Java language itself. This means that it's easy to create your own ClassLoader without having to understand the finer details of the JVM (Java Virtual Machine).

 

Security considerations

The class loader is important in Java's security model because initially, only the class loader knows certain information about classes that have been loaded into the virtual machine. Only the class loader knows where a particular class originated, and only the class loader knows whether or not a particular class was signed (although the class loader arranges for the Class object itself to carry its signature with it). Hence, one of the keys to writing a secure Java application is to understand the role of the class loader and to write (or at least use) a secure class loader.

Whenever you have an application loading arbitrary classes into the system through your class loader, your application's integrity is at risk. This is due to the power of the class loader. 

 



Comments

Popular posts from this blog

Java Stack and Heap: Java Memory Allocation Lesson.

What is JVM, JDK and JRE in Java?

How To Set Getters And Setters Method In java Using Eclipse?