ClassLoader in Java
2 min readDec 25, 2020
Java class loader is a part of the java run time environment that dynamically loads Java classes into the Java virtual environment. The Java run time environment need not need to know about file and file systems because of class loaders. Java classes are not loaded into memory all at once but when they are needed by the application.
Java classloader is of three types Bootstrap classloader, Extension classloader, system classloader.
- BootStrap ClassLoader: A Bootstrap Classloader is a Machine code that 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 the Primodial ClassLoader.
- 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.
- 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.
Methods Of class loader:
- loadClass(String name, boolean resolve): used to load the classes which are referenced by the JVM
- defineClass(): define an array of bytes as an instance of the class.
- findClass(String name): used to find a specified class.
- findLoadedClass(String name): used to verify whether the Class referenced by the JVM was previously loaded or not.
- Class.forName(String name, boolean initialize, ClassLoader loader): This method is used to load the class as well as initialize the class.
That’s all about What is ClassLoader in Java and How it functions. In summary knowledge of How ClassLoader works in Java is a must for any Java developer or architect to design Java application and packaging.