Java ClassLoader
Last updated
Was this helpful?
Last updated
Was this helpful?
When we compile a Java Class, it transforms it in the form of bytecode that is platfrom and machine independent compiled program and store it as a .class file. After that when we try to use a Class, Java ClassLoader loads that class into memory.
There are three types of built-in ClassLoader in Java:
Bootstrap Class Loader
– It loads JDK internal classes, typically loads rt.jar and other core classes for example java.lang.* package classes
Extensions Class Loader
– It loads classes from the JDK extensions directory, usually $JAVA_HOME/lib/ext directory.
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.
Java ClassLoader are hierarchical and whenever a request is raised to load a class, it delegates it to its parent and in this way uniqueness is maintained in the runtime environment. If the parent class loader doesn’t find the class then the class loader itself tries to load the class.
Java default ClassLoader can load files from local file system that is good enough for most of the cases. But if you are expecting a class at the runtime or from FTP server or via third party web service at the time of loading the class then you have to extend the existing class loader. For example, AppletViewers load the classes from remote web server.
When JVM requests for a class, it invokesloadClass
function of the ClassLoader by passing the fully classified name of the Class.
loadClass function calls forfindLoadedClass()
method to check that the class has been already loaded or not. It’s required to avoid loading the class multiple times.
If the Class is not already loaded then it will delegate the request to parent ClassLoader to load the class.
If the parent ClassLoader is not finding the Class then it will invoke findClass() method to look for the classes in the file system.
We will create our own ClassLoader by extending ClassLoader class and overriding loadClass(String name) method.
CCLoader.java: This is our custom class loader with below methods.
private byte[] loadClassFileData(String name)
: This method will read the class file from file system to byte array.
private Class
getClass(String name)
This method will call the loadClassFileData() function and by invoking the parent defineClass() method, it will generate the Class and return it.
public Class
loadClass(String name)
: This method is responsible for loading the Class. If the class name starts with com.journaldev (Our sample classes) then it will load it using getClass() method or else it will invoke the parent loadClass function to load it.
public CCLoader(ClassLoader parent)
: This is the constructor which is responsible for setting the parent ClassLoader.