Oracle9 i AS Release 2 Containers for J2EE

Date: 07/03/02

How-To: Load Local Classes in WAR Module in Preference to Classes loaded at Higher Level

  • The default classloading behavior of Web application classloader.
  • How to configure OC4J to override the system classes and load local classes in the Web module.
  • Build, deploy and run the sample application code.


In the Java2 platform, classloaders are responsible for providing the classes that the Java VM and an application require at runtime. The classloaders use a standard mechanism called the "parent delegation model" to actively find and load the classes. This delegation model uses a hiercachical approach, with there being a root classloader for the whole Java VM, and then a set of surrogate classloaders cascading down through the various layers. Other than the root class loader, each classloader has a parent classloader. This hierachy determines how classes are found and loaded. For example, normally a class loader first tries to get classes from it's parent class loader and if it's not available there, then it tries to load the classes directly from it's code sources. The OC4J Web application classloader follows this delegation model by default. If there are two classes with the same name, for example MyFoo.class packaged in the WAR file and MyFoo.class packaged in a jar file in $OC4J_HOME/j2ee/home/lib directory then MyFoo.class from the JAR file will be used.

In OC4J v9.0.3 and above you can configure the Web application classloader to override this behavior and direct it to load local classes in preference to classes from a higher level. To do this for a specific Web application, you specify the following tag in the orion-web.xml for the Web module. If you wish to have this behavior at a global level you enter the tag in the $OC4J_HOME/j2ee/home/config/global-web-application.xml file.


<web-app-class-loader search-local-classes-first="true" />


Prerequisites for running the example

  •     OC4J v9.0.3 installed

Unpack example files

  • src/ - contains all source code for the example.
    • web/index.jsp - Uses the test.User class as a bean
    • web/test/User - Local test.User class
    • jar/test/User - test.User class that is put in the higher level
  • etc/
    • application.xml - Standard J2EE Application descriptor file.
    • web.xml - Standard Web Application description file.
    • orion-web.xml - OC4J specific Web Application description file.
  • doc/
    • Readme.htm - This document.
  • build.xml - An Ant build file.

Compiling and deploying the application

Using Ant and admin.jar

  • Ensure Ant 1.4.x or above is installed on your machine and configured correctly.
  • Set the JAVA_HOME and OC4J_HOME property value near the top of common.xml (located in the root of the example directory) if these are not picked up from your environment
  • From the root of the example directory [the directory where the build.xml file is located] execute the following command:   ant
  • You should now have a newly created override-sys-classes.ear and override-sys-classes-jar.jar file in your < example_home>\build directory.
  • Copy override-sys-classes-jar.jar to your $OC4J_HOME/home/j2ee/lib directory and start OC4J.
  • Deploy this J2EE application to a running instance of OC4J R2 by executing the following command from the J2EE_HOME directory:

java -jar admin.jar ormi://< host > < uid> < pwd -deploy -file < directory>/ override-sys-classes.ear -deploymentName override-sys-classes

  • Bind the web application by executing the following command from the J2EE_HOME directory:

java -jar admin.jar ormi://< host > < uid> < pwd -bindWebApp override-sys-classes override-sys-classes-web http-web-site /override

  • Test the application by accessing the web app at the URL http://<host>:8888/override/ and you will get the Home Page for the application.
  • Click on the link to run the JSP and you will get an output "Web Module Classloader loaded test.User Class from System loader" that indicates that the local test.User class was not used and it was loaded from the higher level.
  • Change $J2EE_HOME/application-deployments/override-sys-classes/override-sys-classes/orion-web.xml to have the following:
  • <web-app-class-loader search-local-classes-first=" true"/>
  • Reload the application and you will get an output "Web Module Classloader loaded local test.User Class" that indicates that local test.User was used overriding the test.User class at the higher level.


In this exercise you should have learned to:

  • Understand default classloading behavior used in OC4J and how to override this.
  • Build, deploy and run the sample application code.

Left Curve
Popular Downloads
Right Curve
Untitled Document