A Delegation Model (version 1.2 onwards) is used for loading class to the JVM. Class Loaders are structured in java is in a way that at start-up time the JVM doesn’t need to know anything about the classes that will be loaded at run time. The java containers such as EJB or Servlet containers uses custom Class Loaders to support their features like hot deployment and run time platform extensibility..
Class loaders mainly perform two functions. First, when the VM needs to load the byte code for a class, it asks a class loader to find the corresponding byte code. Each class loader will use their own method to find the requested byte code (class),then it loads it from the local disk or fetch them across the Net using any protocol including other classes and interfaces are loaded as they referenced in the byte code being executed, the main class . Then it creates an instance of the java.lang.Class of loaded byte code. This makes the class become available to the JVM for execution. Second, class loaders define the name spaces seen by different classes and how those name spaces relate to each other.
Delegation Model
In delegation model, the class loaders has hierarchical order forming a tree structure,the bootstrap class loader as the root of this tree .
Bootstrap Class Loader.
Java core API classes, which are essential at the JVM load are loaded by the bootstrap (often known as primordial) Class Loader. This class loader loads the class files from local file system using the file accessing options of current OS to open/read . Its read the class files in the form of byte arrays, providing essential resources on the JVM start up. Sometimes this Class Loader is referred as the “internal” class loader or the “default” class loader also.
Extension class loader
The extension class loader is will load classes from the JRE’s extension directories .
System Class Loader
The Application-specific classes are loaded by the system (often known application) Class Loader..
If all the class loader failed to load the the required class, it will generate a java.lang.ClassNotFoundException else a java.lang.NoClassDefFoundError . There is a parent -child relationship is maintained by these class loaders ,except boot strap class loader these class loaders has a parent .
The delegation model follows these steps when the client request for a particular class .
1.JVM first check whether the requested class has already been loaded (JVM caches all the classes that are loaded) by the any Class Loader. If yes, the class is returned.
2.If the class is not loaded, the request is forwarded to the parent Class Loader. This delegation will go up to the bootstrap Class Loader.
3.If the delegation failed to load the class , then current Class Loader will search for the requested class. In its defined locations .
4.If the class loading failed, it will generate a java.lang.ClassNotFoundException .
The Java class loader architecture provide a large amount of flexibility for developers in the application is assembling and extension process.