What is a ClassLoader?
Overview
ClassLoader is a class that is responsible to load classes into JVM’s(Java Virtual Machine) method area.
Basic class loaders are listed below:
- ApplicationClassLoader
- ExtensionClassLoader
- BootStrapClassLoader
ApplicationClassLoader
ApplicationClassLoader to load the classes from the application classpath,(current working directory). It basically uses a classpath environment variable to locate the class i.e. “.class” file.
ExtensionClassLoader
ExtensionClassLoader to load the classes from extension classpath,(%JAVA_HOME%\jre\lib\ext” folder).
BootStrapClassLoader
BootStrapClassLoader to load the classes from BootStrap classpath, (%JAVA_HOME%\jre\lib\rt.jar” folder). These classes are predefined classes.
How ClassLoader works?
Whenever JVM comes across a type, it checks for that class byte code in the method area. If it’s not loaded, it requests the class loader subsystem to load the corresponding class byte code in the method area from that class respective Classpath. Alternatively, if it is already loaded it makes use of that type.
ClassLoader then handovers the request to ApplicationClassLoader, which searches for that class in the folders configured in the Classpath environment variable. If a class is not found, it forwards the request to ExtensionClassLoader and then to BootStrapClassLoader.
If the search fails in all the ClassLoaders then then JVM throws an exception “java.lanf.NoClassDefFoundError” / “java.lang.ClassNotFoundException”.
If class found in any one of the classpaths, the respective ClassLoader loads that class into JVM’s method area.
Please find the below figure for more clarity.
—
Java Tutorials
Java Tutorial on this website:
https://www.testingdocs.com/java-tutorial/
For more information on Java, visit the official website :