Responsive Ads Here

Friday, June 8, 2018

Java: ClassLoader Subsystem

Class loader is a part of JRE that dynamically loads classes in Java Virtual Machine. There is a special class “Class” which contains information about the class. It’s called meta-class. For all object in Java an object of type “Class” is created. Means every time you created a new class, a single object of type “Class” is created. When you create an object of that class, JVM first make sure “Class” for that type is loaded. Once the “Class” object for that type is loaded into memory, it is used create all object of that type. Understand class loaders in Java can help you avoid many problems you might face. For example, if you understand how class loader works in java you can find reason behind NoClassDefFoundError and ClassNotFoundException which are related to loading a class into JVM memory.
Basically ClassLoader is a subsystem which is used to find class into specified path and then serve it to JVM for further use.
There are 3 different type of class loader are available in java which are assigned to load classes from different part of your application.
  1. BootStrap Class Loader: This class loader is responsible for loading class from rt.jar, i18n.jar and others (jars inside lib directory).
  2. Extension Class Loader: This class loader is responsible for loading class from lib/ext directory.
  3. Application Class Loader: This class loader is responsible for loading class from classpath. Class path might be set through system property java.class.path or from command line using -cp or -classpath both have same effect or by setting CLASSPATH environment variable.
The another type of class loader can be considered Custom Class Loader. We will discuss custom class loader in the last of this post.
Principles: Class Loaders follow 3 simple principle while loading any class.
Delegation: Each class loader except BootStrap loader has a parent class loader. Before loading any class, class loader delegates request to parent class loader. And parent class loader delegates request to its parent. When parent class loader unable to find class it delegates request to child class loader and so on.
CL-Subsystem
Uniqueness: Class loader subsystem follow uniqueness principle. That means only one class loader can load a class. And one class can be loaded only once.
Visibility: Visibility is responsible for Uniqueness. Whenever any class loader loads a class it make sure, loaded class is visible to all its children. So every child is aware of classes loaded by any parent class but no parent class knows about the class loaded by any child.
Class Loading: While loading any class, ClassLoader follow three basic principles. Whenever any class is referenced, Application ClassLoader check whether class is already loaded or not. If not it delegate request to Extension ClassLoader. Extension then check whether class is loaded or not if not, it delegate request to BootStrap ClassLoader. Now BootStrap ClassLoader try to check first whether class is loaded or not. If not then BootStrap ClassLoader try to load class, if BootStrap ClassLoader could not load the class, it delegate request to Extension ClassLoad to load class, if Extension ClassLoader is also unable to load class, it delegate request again to Application ClassLoader, then Application ClassLoader tries to load class. If Application ClassLoader is also unable to load class, ClassNotFoundException occurred.

No comments:

Post a Comment