# JOnAS class loader hierarchy

## Target Audience and Content

The target audience for this guide is the EAR application provider, i.e. the person in charge of developing the J2EE application components on the server side. This guide describe a key feature of the J2EE integration : the introduction of class loader hierarchy in JOnAS. So this guide present this important new feature and the utility of this feature.

The content of this guide is the following:

1. Target Audience and Content
2. Understanding class loader hierarchy
3. Bootstrap class loader
4. JOnAS class loaders
5. Conclusion

## Understanding class loader hierarchy

When an application is deployed, it is done by its own class loader. This means that if you deploy for instance a WAR and an EJB JAR separately, the classes contained in the two archives are loaded with two separate classloaders with no hierarchy between them. So, the Web components in the WAR will not have the visibility of the EJBs from within the JAR.
This is not acceptable in case of applications were the Web components need to reference and use some EJBs.

For this reason, prior to EAR files, when needed to deploy a Web application using EJBs, the EJB JAR had to be placed in the WEB-INF/lib directory of the Web application.

Currently, with the J2EE integration and the use of the EAR packaging, there are no more class visibility problems, and the EJB JAR is not required anymore in the WEB-INF/lib directory.

The next sections describe the JOnAS class loader hierarchy to explain the mecanism used to find the referenced classes.

## Bootstrap class loader

The bootstrap class loader is a JOnAS specific class loader that will load all required classes to start the JOnAS server. This class loader has the system class loader as parent class loader. It adds all the required libraries to start the JOnAS server (libraries for the mail, tomcat, ...). Also, il loads the classes located in `XTRA_CLASSPATH`. For example, when loading an unpacked EJB component, the location of its classes must be specified in the `XTRA_CLASSPATH`.

In order to have libraries available by each component running inside JOnAS, you can add in the `JONAS_ROOT/lib/ext` directory your required jars file. Don't forget to do an `'ant install'` in the `JONAS_ROOT` directory after this.
If you need a specific jar only for a web application (need to use a version of a jar file which is different than a version loaded by JOnAS) you can change the compliance of the web application classloader to the java 2 delegation model. See below : WEB class loader.
You can also use the extension mechanism which is described in the section dependencies of the J2EE specification (section 8.1.1.28)

## JOnAS class loaders

The JOnAS class loader hierarchy that allows the deployment of EAR applications without placing the EJB JAR in the WEB-INF/lib directory is composed of:

### EAR class loader

The EAR class loader is responsible of loading the EAR application. There is only one EAR class loader per EAR application and this class loader is the child of the bootstrap class loader to have the visibility of the JOnAS classes.

### EJB class loader

The EJB class loader is responsible of loading all the EJB JARs of the EAR application, so all the EJBs of the same EAR application are loaded with the same EJB classloader. This class loader is the child of the EAR class loader.

### WEB class loader

The WEB class loader is responsible of loading the Web components. There is one WEB class loader per WAR file, and this class loader is the child of the EJB class loader. Using this class loader hierarchy (the EJB class loader is the parent of the WEB class loader), there is no more problem of classes visibility when a WEB component try to reference some EJBs. Indeed the classes loaded with the WEB class loader have the visibility of the classes loaded by its parent class loader (EJB class loader).

The compliance of the class loader of the web application to the java 2 delegation model can be change by using the jonas-web.xml file. This is described in the section : Customizing jonas-web.xml file
If the java2-delegation-model element is set to false, the class loader of the web application lookup the class in its own repository before asking its parent class loader.

## Conclusion

The JOnAS resulting class loader hierarchy is the following: