Introduction:what is Classloader
All classes files in application is not loaded to memory at startup, but is loaded on demand as needed by the program, T His loading of class are done by ClassLoader. ClassLoader is a part of the JVM that loads classes into memory.
The Java ClassLoader is written in Java itself (opposite to JVM which are written in C + +). Which means that it's easy-to-develope custom Class Loader when ever required without have to understand the details of The JVM. ClassLoader is an abstract class which are the part of the Java.lang package.
How Classloader Works
An application might require different classes to function properly at runtime, whenever an application requires a Class A T runtime, ClassLoader'll look for the given class name and if found it'll load it in memory. Once provided the name of class to load, ClassLoader would try to locate data that constitue the definition of class at di Fferent locations.
Location searched by the ClassLoader would be in given order.
- First look for the classes in JAR files in the Lib/ext directory of the JRE, and in the System-wide, platform-specific ext Ension directory.
- Second look for the class in Classpath (Java.class.path property). Default value of the classpath is current directory and this value can be changed in different.
ClassLoader Hierarchy
ClassLoader uses a delegation model to search for classes and resources to load. Each instance of ClassLoader'll has an associated parent class loader. When a ClassLoader GET request to find a class, given ClassLoader would delegate the search for the class or Resou Rce to it parent class loader before attempting to find the class or resource itself. Bootstrap class Loader is on the top of class Loader hierarchy. Bootstrap ClassLoader is the built in class loader in virtual machine. Bootstrap class loader doesn ' t has any parent.
Type of Classloader
Java ClassLoader can divided in 4 types.
Bootstrap Class Loader
It is the Super Class Loader. It doesn ' t has any parent ClassLoader. It loads the Core classes of Java like class in Java.lang, Java.util.
Extension Class Loader
Extension ClassLoader would load the classes in JAR files in the Lib/ext directory of the JRE, and in the System-wide, plat Form-specific extension directory. Bootstrap ClassLoader would be the parent of Extension ClassLoader. Developer can manually add jars in ext folder to get it loaded by Extension ClassLoader.
System Class Loader
System ClassLoader would load the classes in Classpath (java.class.pathproperty). Extension ClassLoader works as the parent of System ClassLoader. Default value of the classpath is current directory and this value can be changed in different.
- Setting the CLASSPATH enviornment variable.
- Value providing by-classpath OR-CP command-line options.
Custom Class Loader
Develoeper can also create Custom class according to there need.
Advantage of Using Custom ClassLoader
- Hotdeployment in Application Server
- Custom ClassLoader in case of browser to check for security certification before executing untrusted code.
Java Class Loader