Share a library across multiple Java EE applications
Libraries can be shared across multiple Java EE applications. All the applications can use the same classes at run time, or each application can use its own separate copy of those classes loaded from the same location.
In the following example, a library called Alexandria consists of two files:
- alexandria-scrolls.jar and
- commons-lang.jar
An application called Scholar and an application called Student are running on a server called Academy, and both need access to this library.
- Create a mylib/Alexandria directory in the servers/Academy directory under the ${WLP_USER_DIR} directory.
For example: wlp/usr/servers/Academy/mylib/Alexandria.
- Copy the alexandria-scrolls.jar and commons-lang.jar files into the new folder.
- Configure class loading for the application, so the Alexandria library is loaded.
In server.xml, or an included file, define the library by adding the following code:
<library id="Alexandria"> <fileset dir="${server.config.dir}/mylib/Alexandria" includes="*.jar" scanInterval="5s" /> </library>The <library> element can also take a filesetRef attribute with a comma-separated list of <fileset> element IDs.
- Reference the library from the applications, so that both these applications share a single copy of the library.
In server.xml, or an included file, add the following code:
<application id="scholar" name="Scholar" type="ear" location="scholar.ear"> <classloader commonLibraryRef="Alexandria" /> </application> <application id="student" name="Student" type="ear" location="student.ear"> <classloader commonLibraryRef="Alexandria" /> </application>The <commonLibraryRef> element can take a comma-separated list of library IDs.
- Optional: Configure another application to have its own set of classes loaded from the same JAR files.
For example, if another application called Spy needs its own copy of the classes, the same physical files on disk can be used. In server.xml, or an included file, add the following code:
<application id="spy" name="Spy" type="war" location="spy.war"> <classloader privateLibraryRef="Alexandria" /> </application>The <privateLibraryRef> element can take a comma-separated list of library IDs.
Parent topic: Configure class loaders and libraries for Java EE applicationsConcepts:
Shared libraries Tasks:
Use a Java library with a Java EE application Provide global libraries for all Java EE applications Access third-party APIs from a Java EE application Remove access to third-party APIs for a Java EE application Overriding a provided API with an alternative version
Defining a utility project as a shared library
Set a web project to use shared libraries