Share a library across multiple Java EE applications


Overview

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


Steps

  1. Create directory to hold lib and copy libs into place...

      mkdir ${WLP_USER_DIR}/servers/Academy/mylib/Alexandria
      cp $alexandria-scrolls.jar commons-lang.jar {WLP_USER_DIR}/servers/Academy/mylib/Alexandria

  2. In server.xml, or an included file, define the library...

      <library id="Alexandria">
        <fileset dir="${server.config.dir}/mylib/Alexandria" includes="*.jar" scanInterval="5s" />
      </library>

    Note that the <library> element can also take a filesetRef attribute with a comma-separated list of <fileset> element IDs.

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

    Note that the <commonLibraryRef> element can take a comma-separated list of library IDs.

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

    Note that the <privateLibraryRef> element can take a comma-separated list of library IDs.


See also: Shared libraries


Parent topic: Configure class loaders and libraries for Java EE applications


Related tasks