The Class Loader in Java plays a crucial role in the Java runtime environment by dynamically loading Java classes into the Java virtual machine. Its primary responsibility is to locate libraries, read their contents, and load the classes within those libraries.
The presence of class loaders eliminates the need for the Java runtime system to have knowledge about files and file systems. Instead, the class loader takes care of these operations, facilitating the loading of classes during runtime.
In Java, three class loaders are utilized when the JVM is initiated:
- Bootstrap class loader
- Extension class loader
- System class loader
1) Bootstrap class loader
The Bootstrap class loader is responsible for loading the fundamental Java libraries located in the JRE Library directory. It handles the essential components necessary for the functioning of the Java runtime environment.
2) Extension class loader
The Extension class loader loads classes found in the extension directory, extending the capabilities of the Java runtime environment beyond the core libraries.
3) System class loader
The System class loader, starting with the class path, loads all the classes required for the execution of a Java program.
When the JVM needs to locate a specific class, it first checks with the System class loader. If the class is not found, it then consults the Extension class loader. If the class is still not found, the JVM finally searches the Bootstrap class loader. If the class is not found after these steps, a “ClassNotFound” exception is thrown, indicating that the class could not be located.
In conclusion, the Class Loader in Java is an integral component of the Java runtime environment, responsible for dynamically loading Java classes into the JVM. By understanding the role and functionality of the Class Loader, developers can harness the flexibility and adaptability of Java applications.