|
Page 1 of 2
.
WebSphere V5.0 : Web Development - Understand classpath and module dependencies
In this tutorial you will learn about WebSphere V5.0 Web Development - Understand classpath and module dependencies - Specifying dependent JAR files or modules, Class loaders, Class-loader isolation policies, Class-loader modes, Editing a server configuration, Setting class loader policy for a server and Setting class loader policy for an application on a server
Specifying dependent JAR files or modules
Use the JAR Dependencies editor to specify JAR files or modules that are required by a module.
In order to select required JAR files or modules, you must specify the enterprise application to which your project is scoped. Usually, the project will be referenced by one EAR project in the workspace. However, it is possible that you will have multiple applications that contain a reference to the same module or utility JAR project. If this is the case, then you should ensure that you give the JAR or module the same URI in each application, so that the classpath will be valid for all applications.
To select JAR files or modules that are required by a module:
1. Switch to the J2EE perspective.
2. In the J2EE Hierarchy view, right-click the desired module, and click Open With > JAR Dependency Editor from the context menu.

3. In the Classpath Scope section on the Dependencies page of the editor, select the enterprise application to use for classpath editing. The JAR dependency editor is designed for classpath editing. Therefore, not all of the attributes or information contained in the manifest file (MANIFEST.MF) will be represented on the this page. The Classpath Scope section lists all enterprise applications that contain a reference to the selected project as a module or utility JAR. Use the Refresh button to update the list if a change is made in a separate editor.
4. In the Dependencies section, select the JAR files or modules that are required or dependent. You can also move them up and down in the list to specify their order on the MANIFEST class path as well as the Java build path.

5. Click File > Save to save your changes.
TIP: Referencing run-time JAR files at development time: If you need to compile against JAR files visible to the WebSphere Application Server runtime (for example, rt.jar, xerces.jar, and so on), then as a general rule, you should add those JAR files for the respective runtime installation using the predefined class path variables. For example, you should use the WAS_PLUGINDIR class path variable for the WebSphere Application Server V4.0 server JAR files and the WAS_50_PLUGINDIR class path variable for the WebSphere Application Server V5.0 server JAR files.
Class loaders
Class loaders are part of the Java virtual machine (JVM) code and are responsible for finding and loading class files. Class loaders affect the packaging of applications and the run-time behavior of packaged applications deployed on application servers.
The run-time environment of WebSphere Application Server uses the following class loaders to find and load new classes for an application in the following order:
1. The bootstrap, extensions, and CLASSPATH class loaders created by the JVM.
The bootstrap class loader uses the boot classpath (typically classes in jre/lib) to find and load classes. The extensions class loader uses the system property java.ext.dirs (typically jre/lib/ext) to find and load classes. The CLASSPATH class loader uses the CLASSPATH environment variable to find and load classes.
The CLASSPATH class loader contains the J2EE APIs of the WebSphere Application Server product (inside j2ee.jar). Because the J2EE APIs are in this class loader, you can add libraries that depend on J2EE APIs to the classpath system property to extend a server's classpath. However, a preferred method of extending a server's classpath is to add a shared library.
2. A WebSphere-specific extensions class loader.
The WebSphere extensions class loader loads the WebSphere run-time and J2EE classes that are required at run time. The extensions class loader uses a ws.ext.dirs system property to determine the path used to load classes. Each directory in the ws.ext.dirs classpath and every JAR file or ZIP file in these directories is added to the classpath used by this class loader.
The WebSphere extensions class loader also loads resource provider classes into a server if an application module installed on the server refers to a resource that is associated with the provider and if the provider specifies the directory name of the resource drivers.
3. One or more application module class loaders that load elements of enterprise applications running in the server.
The application elements can be Web modules, EJB modules, resource adapters, and dependency JAR files. Application class loaders follow J2EE class-loading rules to load classes and JAR files from an enterprise application. The WebSphere run time enables you to associate a shared library classpath with an application.
Each class loader is a child of the class loader above it. That is, the application module class loaders are children of the WebSphere-specific extensions class loader, which is a child of the CLASSPATH Java class loader. Whenever a class needs to be loaded, the class loader usually delegates the request to its parent class loader. If none of the parent class loaders can find the class, the original class loader attempts to load the class. Requests can only go to a parent class loader; they cannot go to a child class loader. If the WebSphere class loader is requested to find a class in a J2EE module, it cannot go to the application module class loader to find that class and a ClassNotFoundException occurs. Once a class is loaded by a class loader, any new classes that it tries to load reuse the same class loader or go up the precedence list until the class is found.
Class-loader isolation policies
The number and function of the application module class loaders depends on the class-loader policies specified in the server configuration. Class loaders provide multiple options for isolating applications and modules to enable different application packaging schemes to run on an application server.
Two class-loader policies control the isolation of applications and modules:
- Application class-loader policy
.
Application class loaders consist of EJB modules, dependency JAR files, resource adapters, and shared libraries. Depending on the application class-loader policy, an application class loader can be shared by multiple applications (SINGLE) or unique for each application (MULTIPLE). The application class-loader policy controls the isolation of applications running in the system. When set to SINGLE, applications are not isolated. When set to MULTIPLE, applications are isolated from each other.
.
- WAR class-loader policy
.
By default, Web module class loaders load the contents of the WEB-INF/classes and WEB-INF/lib directories. The application class loader is the parent of the Web module class loader. You can change the default behavior by changing the application's WAR class-loader policy.
.
The WAR class-loader policy controls the isolation of Web modules. If this policy is set to APPLICATION, then the Web module contents also are loaded by the application class loader (in addition to the EJB files, RAR files, dependency JAR files, and shared libraries). If the policy is set to MODULE, then each web module receives its own class loader whose parent is the application class loader.
Note: WebSphere server class loaders never load application client modules.
For each application server in the system, you can set the application class-loader policy to SINGLE or MULTIPLE. When the application class-loader policy is set to SINGLE, then a single application class loader loads all EJB modules, dependency JAR files, and shared libraries in the system. When the application class-loader policy is set to MULTIPLE, then each application receives its own class loader used for loading that application's EJB modules, dependency JAR files, and shared libraries.
This application class loader can load each application's Web modules if that WAR module's class-loader policy is also set to APPLICATION. If the WAR module's class-loader policy is set to APPLICATION, then the application's loader loads the WAR module's classes. If the WAR class-loader policy is set to MODULE, then each WAR module receives its own class loader.
The following example shows that when the application class-loader policy is set to SINGLE, a single application class loader loads all EJB modules, dependency JAR files, and shared libraries of all applications on the server. The single application class loader can also load Web modules if an application has its WAR class-loader policy set to APPLICATION. Applications having a WAR class-loader policy set to MODULE use a separate class loader for Web modules.
Application class-loader policy: SINGLE
Application 1
.....Module: EJB1.jar
.....Module: WAR1.war
..........MANIFEST Class-Path: Dependency1.jar
..........WAR Classloader Policy = MODULE
Application 2
.....Module: EJB2.jar
..........MANIFEST Class-Path: Dependency2.jar
.....Module: WAR2.war
..........WAR Classloader Policy = APPLICATION

|