The role of a class loader in Java is to hide the details of loading classes - like searching the file system (local as well as network) for the class file, loading the class file, returning it to JVM as a Class class so that JVM can use the Class class to instantiate the requested object in the application. Since J2SE, the JVM is provided 3 distinct primary classloaders -
The above 3 exist in a parent-child relationship as follows:
A delegation model is utilized by the JVM in order to determine which class loader amongst the above 3 to use to load a particular class. The model works in a "Delegate to Parent-Before-Looking" as follows: When an application requests a Class (e.g String str = "Hello World"; or MyClass myobj = new MyClass()) :
- Bootstrap class loader - This class loader is written in native language and comes as part of the JVM implementation. It loads all the core Java classes. (e.g java.lang.* etc). The location of these jars depends on the implementation of JVM. Sun's JVM looks in the jdk/jre/lib directory.
- Extension class loader - Usually, developers make use of the CLASSPATH environment variable to load application classes, 3rd party jars etc. However, the CLASSPATH can become too unwieldy to handle and prone to errors using this approach. So since Java 1.2 , we can drop the 3rd party jars into a standard extension directory - jdk/jre/lib/ext - and JVM will find them. The extension class loader loads all the classes found in one or more of these extension directories.
- System class loader - This class loader locates and loads the classes in the directories and jar files specified on the CLASSPATH variable. It also loads the application's main class (the one containing the main() method).
The above 3 exist in a parent-child relationship as follows:
A delegation model is utilized by the JVM in order to determine which class loader amongst the above 3 to use to load a particular class. The model works in a "Delegate to Parent-Before-Looking" as follows: When an application requests a Class (e.g String str = "Hello World"; or MyClass myobj = new MyClass()) :
- Each class loader delegates the request to its parent.
- Once the topmost class loader is reached, the bootstrap class loader, it tries to load the class. If it is unable, its child will try.
- If one of the class loaders finds the class, it is returned as a Class object. Following the delgation pattern , if the lowermost class loader in the hierarchy (System class loader) does not find the class, a ClassNotFoundException is thrown.
2 comments:
classloader looks class in cache before delegating lookup to parent
This class loader have great advantages. And I think this is the best among all.
.net obfuscators
Post a Comment