The Java ClassLoader is integral part of Java Virtual Machine (JVM), and is responsible for finding and loading class files during runtime. We can also create our own custom class loader to extend the functionality of JVM. In this article, we’ll discuss about class loaders, how they work and the methods invoked during class loading process.
What is a ClassLoader
In Java, programmer written code is compiled into platform independent format, that is as a .class file. Each Java program consists of many individual .class files, each of which relates to a single Java class. These class files are not loaded into memory all at once, instead these class files are loaded on demand basis whenever needed by the program. So the ClassLoader’s primary responsibility is to load these classes into memory. Since the ClassLoader itself is written in Java programming language, it is easier to write our own custom ClassLoader if need arises to have finer control over class loading process. Usually the exception ClassNotFoundException is associated with ClassLoader not able to find and load the class which was referenced from other classes.
Types of ClassLoaders
ClassLoaders are broadly categorized into three types.
Bootstrap class loader responsible for loading Java core classes from core packages like java.lang.
Extension class loader loads classes from library extension directory, usually located at JAVA_HOME/jre/lib/ext. This directory contains .jar files which contains class files those are extensions to core Java classes.
Classpath class loader loads Java classes available at the system classpath.
The classes are loaded into the memory in the order mentioned above, that means first bootstrap classes are loaded, then extension classes are loaded and finally classes from classpath are loaded.
Sometimes the default class loading functionality may not be sufficient for certain business requirements. In this kind of situations, we have an option to write our own custom class loaders. So apart from loading classes from local disk or from remote network, custom class loaders can do tasks like verifying digital signature of untrusted code before executing etc. Having custom class loader will gives us finer control over class loading.
The primary responsibility of class loader is to serve class file. JVM requests ClassLoader for a class, and the ClassLoader finds the class and returns a ‘Class’ object that belongs to the requested class. We can create custom ClassLoader by overriding methods of class loading process. The following sections discuss about the steps in the class loading process and the methods invoked.
Methods involved in class loading process
- ClassLoader.loadClass(String name, boolean resolve) method is the entry point for the ClassLoader. The name argument is the name of the fully qualified class JVM requested, and the argument resolve indicates whether the class needs to be resolved. This resolution flag logically prepares the class for execution. This flag can be false if JVM only needs to check the existence of class.
- defineClass() method is the core of ClassLoader. This method takes the raw bytes loaded form local file system or from network and converts them into a Class object. This method does complex processing by parsing bytecode into runtime code. This method is marked final so we cannot override this method. The default implementation of this method is sufficient.
- findSystemClass() method loads the files from local file system by finding the class file If the class file is available, then the class file from raw bytes will be turned into Class object using above defineClass method.
- resolveClass() method resolves the loaded class. Depending on the value of the argument resolve in the loadClass method, this method is invoked to prepare the class for execution.
- findLoadedClass acts as a cache. When the loadClass if invoked, it will check whether the class is already loaded by this ClassLoader by calling this method. If the class file is not loaded previously, actual loading process takes place.
All in all, the following are the sequence of steps in a class loading process.
- findLoadedClass() is called to see whether the class is already loaded.
- If class is not already loaded, and if we don’t have raw bytes, we call findSystemClass() to get raw bytes from local file system.
- Once raw bytes are loaded, call to defineClass() will turn raw bytes into Class object.
- If the resolve argument is true, call to resolveClass() will resolves the Class object and prepares for execution.
- Return the class to the caller.
- If class is not available to load, a ClassNotFoundException is thrown.
- Thread States In Java - July 1, 2014
- Uses Of Synchronized Keyword In Java - June 27, 2014
- Java ClassLoaders - June 24, 2014
- Importance Of Overriding Equals And Hashcode Methods - June 18, 2014
- Implementing Threads in Java - June 17, 2014