Understanding Class Loader- Web logic perspective

June 27, 2009

Introduction

 Class loaders are runtime environment that dynamically loads into virtual machine. Both Java and .Net adopts the same principle in loading the class using class loader, where as the implementation of these are different. Java IL is type safe where as .Net IL is type-neutral.   Since .Net is type-neutral   and makes IL is easier to translate into native code, which is disadvantage on performance since types are identified using reflection and advantage since it is more generic. This column focuses the class loader on Java perspective and Web logic application server defined in J2EE specification.

J2EE Specification – In Brief

 Java enterprise specification describes three modules: Web, Enterprise JavaBeans and application client modules. While deploying J2EE application in application server, all of these modules are compressed into a single file with extension as enterprise architecture (EAR) file.

Web Application Module

The web application module contains HTML, JSPs, Java classes, servlets, images and other resources required for web application.  The web.xml is a deployment descriptor file, has servlet initializations and mapping information and other settings for running the web application within the application server. The web application module has two special folders for Java code: WEB-INF/classes’ and WEB-INF/lib and contains loose Java classes (classes that are not in JAR file), and usually utility classes used by servlet, a special class loader are used for this folder, so that any changes in this folder are automatically reloaded by the application server. The web project contains two folders the ‘Source’ and the ‘Application’. The application folder contains web archive file (WAR file ) and the source contains .java file usually not part of WAR file.  While importing a WAR file, make sure you delete the .java file and it is redundant.

EJB Module

 An EJB module contains EJBs, the server-specific deployment code, descriptor and optional header classes’. The EJB module contains two folders namely ejbmodule and bin folder. As you do changes in the .java file which are in ejbmodule, automatically compiled and placed in bin folder. Adopt the same principle by removing .java file in Jar while importing an EJB module.

Class Loader– In Brief


As defined earlier, it is a mechanism of loading classes into the JVM, it is subclass of java.lang.ClassLoader and it is hierarchical. Each class loader other than the initial loaders has a parent class loader. The root parent class loader is built into JVM and is called the ‘bootstrap’ class loader, so a class loader can have multiple child class loaders; it is possible to have sibling class loaders at the same level of hierarchy.  A class loader while loading a class themselves, delegate the load request to its parent and moves into the hierarchy until it reaches the root.  The child attempts load the requested class only when parent cannot resolve it. Child class loaders can see the classes loaded by the parent whereas parent cannot see the class loaded by the child. Classes can be unloaded by the class loader, but class loaders themselves cannot be unloaded. Classes are referenced as classloader.classname. The class can exist either on file system, remote server or in database.

As defined before a typical JVM has bootstrap class loader (a primordial class loader), extension class loader and system class loader. The JVM loads the bootstrap class loader and loads the classes that are defined in JDK. Extensions class loader loads the Java runtime classes. System class loader loads the classes that are defined in classpath, shared libraries and the Jar. Classes are loaded when new byte code is executed for the first time, either via a new class instance or by a first reference made to a static class by the class loader or custom class loader that extends the class loader

Class Loader– In Web Logic


 According to support the J2EE standard, Weblogic server defines and initializes a number of its own classloaders and is hidden form developer perspective, to manage loading and unloading your classes, EARs, EJBs and WARs and your own custom class loader. Class loading and deployment are inseparable. Deploying module in Weblogic, each is typically loaded in its own dynamic classloader. Every single EJB JAR file gets its own classloader, a WAR file gets one, and the EAR file gets sets of classloaders based on what is in it.  In Weblogic application deployment unit are EAR , an EJB  or a WAR. When they are deployed separately they are loaded in separate sibling classloaders. If EJB and WAR grouped into EAR, they get hierarchy of class loaders. The Weblogic and JBOSS implements different principle and they have their pattern of custom classloaders.

Conclusion

Weblogic adopts and conforms to the behaviroal requirement based on J2EE specification, employ different implementation of loading classes using classloader.


My First oppurtunity to abroad

April 2, 1996

Srikanth and Shankar guys from Arkin System met me in T.Nagar, Chennai, India for an abroad opportunity for a project in US. We had a long discussion and outcome of it is become an employee of Arkin.