Sharing 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.

Open Liberty The latest documentation about class loader configuration for Liberty is available on the Open Liberty website.

About this task

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.

Procedure

  1. Create a mylib/Alexandria directory in the servers/Academy directory under the ${WLP_USER_DIR} directory.

    For example: wlp/usr/servers/Academy/mylib/Alexandria.

  2. Copy the alexandria-scrolls.jar and commons-lang.jar files into the new folder.
  3. Configure class loading for the application, so that the Alexandria library is loaded.
    In the server.xml file, 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>
    Note: The <library> element can also take a filesetRef attribute with a comma-separated list of <fileset> element IDs.
  4. Reference the library from the applications, so that both these applications share a single copy of the library.
    In the server.xml file, 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>
    Note: The <commonLibraryRef> element can take a comma-separated list of library IDs.
  5. 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 the server.xml file, or an included file, add the following code:
    <application id="spy" name="Spy" type="war" location="spy.war">
      <classloader privateLibraryRef="Alexandria" />
    </application>
    Note: The <privateLibraryRef> element can take a comma-separated list of library IDs.