Class loaders
Overview
Class loaders enable applications deployed on servers to access repositories of available classes and resources. Application developers must consider the location of class and resource files, and the class loaders used to access those files, to make the files available to deployed applications.
Class loaders used and the order of use
WAS ND v7.0 runtime environment uses the following class loaders to find and load new classes for an application in the following order:
- The bootstrap, extensions, and CLASSPATH class loaders created by the Java virtual machine
Class loader Uses... bootstrap Boot class path (jre/lib) extensions System property java.ext.dirs (jre/lib/ext) CLASSPATH CLASSPATH environment variable The CLASSPATH class loader loads the Java EE APIs provided by WAS v7 in j2ee.jar. We can add libraries that depend on the Java EE APIs to the CLASSPATH to extend a server class path. The preferred method is to add a shared library.
- WebSphere extensions class loader
Load WAS classes required at run time, using a ws.ext.dirs system property to determine the path used to load classes. Each directory in the ws.ext.dirs class path and every JAR file or ZIP file in these directories is added to the class path.
Also loads resource provider classes into a server if...
- An application module installed on the server refers to a resource associated with the provider
- The provider specifies the directory name of the resource drivers
- Application module class loaders
Loads elements of enterprise applications running in the server. The application elements can be...
- Web modules
- EJB modules
- RAR files
- dependency JAR files
Application class loaders follow Java EE class-loading rules to load classes and JAR files from an enterprise application. WAS v7 enables you to associate shared libraries with an application.
- Web module class loaders
By default, loads the contents of...
- WEB-INF/classes
- WEB-INF/lib
Web module class loaders are children of application class loaders. We can specify that an application class loader load the contents of a Web module rather than the Web module class loader.
Each class loader is a child of the previous class loader. That is, the application module class loaders are children of the WebSphere 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 extensions class loader is requested to find a class in a Java EE module, it cannot go to the application module class loader to find that class and a ClassNotFoundException error occurs.
After a class is loaded by a class loader, any new classes that it tries to load either...
- Reuses the same class loader
- Goes up the precedence list until the class is found
Class-loader isolation policies
The number and function of the application module class loaders depend 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 appserver.
Two class-loader policies control the isolation of applications and modules:
Class-loader policy Description Application Application class loaders load...
- EJB modules
- dependency JAR files
- embedded resource adapters
- application-scoped shared libraries
Depending on the application class-loader policy, an application class loader can be...
- Shared by multiple applications (Single)
- Unique for each application (Multiple)
The application class-loader policy controls the isolation of applications running in the system.
Class-loader policy Isolation... Single Applications are not isolated from each other Multiple Applications are isolated from each other WAR By default, Web module class loaders load the contents of...
- WEB-INF/classes
- WEB-INF/lib
The application class loader is the parent of the Web module class loader. We can change the default behavior by changing the WAR class-loader policy of the application.
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...
- EJB files
- RAR files
- dependency JAR files
- 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.
The console and deployment.xml use different names for WAR class-loader policy values. In the console, the WAR class-loader policy values are Application or Module. In deployment.xml the WAR class-loader policy values are Single or Multiple.
- Application is the same as Single
- Module is the same as Multiple
Restriction: WAS class loaders never load application client modules.
For each appserver in the system, we 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 the EJB modules, dependency JAR files, and shared libraries for that application.
An application class loader loads classes from Web modules if the application's WAR class-loader policy is set to Application. If the application's 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 of the 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 that have a WAR class-loader policy set to Module use a separate class loader for Web modules.
Server's application class-loader policy: Single Application's WAR class-loader policy: ModuleApplication 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
The following example shows that when the application class-loader policy of an appserver is set to Multiple, each application on the server has its own class loader. An application class loader also loads its Web modules if the application WAR class-loader policy is set to Application. If the policy is set to Module, then a Web module uses its own class loader.
Server's application class-loader policy: Multiple Application's WAR class-loader policy: ModuleApplication 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
Class-loader modes
The class-loader delegation mode, also known as the class loader order, determines whether a class loader delegates the loading of classes to the parent class loader.
The following values for class-loader mode are supported:
Class-loader mode Description Parent first Also known as Classes loaded with parent class loader first.
The Parent first class-loader mode causes the class loader to delegate the loading of classes to its parent class loader before attempting to load the class from its local class path. This value is the default for the class-loader policy and for standard JVM class loaders. Parent last Also known as Classes loaded with local class loader first or Application first.
The Parent last class-loader mode causes the class loader to attempt to load classes from its local class path before delegating the class loading to its parent. Using this policy, an application class loader can override and provide its own version of a class that exists in the parent class loader.
The following settings determine the mode of a class loader:
- If the application class-loader policy of an appserver is Single, the server-level mode value defines the mode for an application class loader.
- If the application class-loader policy of an appserver is Multiple, the application-level mode value defines the mode for an application class loader.
- If the WAR class-loader policy of an application is Module, the module-level mode value defines the mode for a WAR class loader.
Related concepts
Enterprise (Java EE) applications
Related tasks
Create shared libraries
Manage shared libraries
Related
Enterprise application settings
Application server settings
Shared library collection