Class Loader and Java /J2EE Application Packaging

Just a step forward to discuss about java and j2EE class loader and Java/J2EE application packaging. Mainly on the complex J2EE application packaging.

Thursday, November 09, 2006

Understanding Class Loading Basics From Packaging Point of View

The Java / J2EE application packaging basics lies in understanding the class loading architecture of J2EE application servers or servlet containers. Understanding the details of J2EE class loading architecture will help in understanding and analyzing different approaches of packaging J2EE application.
Class loading is the process of locating the bytes for a given class and converting them into a Java Class instance. All java virtual machine's has a built-in class loader object, called the "bootstrap class loader" that starts the process of class loading.
A Class Loader object is an instance of a child class of the abstract class java.lang.ClassLoader. The ClassLoader class uses a delegation model to search for classes and resources. Each instance of ClassLoader has an associated parent class loader except the root ClassLoader object. When a program or application requests to find a class or resource, a ClassLoader instance delegates the search for the class or resource to its parent class loader before attempting to find the class or resource itself. If the class loader at the top of the hierarchy cannot fulfill the request, then the child class tries to load the class. If the child is also unable to load the class, the request continues back down the hierarchy until a class loader fulfills it or the ClassLoader at the bottom of the hierarchy throws ClassNotFoundException.
Note : Some of the application server provides option to change this delegation mode from parent first to parent last mode.
Each ClassLoader defines it's own unique and separate namespace. The classes that are included in this name space are the classes that are loaded by the ClassLoader instance itself and the classes that are loaded by it's parent ClassLoaders in the hierarchy. A ClassLoader can have only one instance of a given Class in it's namespace. But the same class can be loaded into the namespace of another ClassLoader.
Let's take an example.

In the above diagram ClassLoaderB's name space included the classed loaded by Bootstrap ClassLoader, Classloaded by ClassLoaderA and the classes loaded by itself. If the ClassLoaderB loads the class 'MyClass' then the same can also be loaded again by ClassLoaderC. But a class 'OtherClass' is loaded by ClassLoaderA then the same Class is visible to both ClassLoaderB and ClassLoadereC's namespace and they will not load the class 'OtherClass' again to it's namespace.

0 Comments:

Post a Comment

<< Home