Search code examples
javaclasssharedearclassloader

loading shared classes in application javaee5


I am trying to load some classes that are common to all the web applications of my ear in a java ee 5 application.

I tried to do this by putting the classes (not jar) in a) directory called "lib"

b) also specifying in application.xml's

   <module><java>lib/common.jar</java></module> 

and was not successful by either option a or b

but when I jarred the classes into common.jar, I was able to load the classes by method b)

  1. Do both these method need the classes to be jarred ?
  2. what is the difference between providing the classes via the above 2 methods ? why does it seem like there are two ways to specify loading common classes ?

Solution

  • I'm not sure which application server is being referred to here, and the nature of the common.jar file. For now, I'll assume that the application server is any Java EE 5 container, and that the common.jar file is a utility jar (and not an EJB or similar module).

    The Java EE 5 Platform Specification actually defines the manner in which library support is to be provided by containers:

    A .ear file may contain a directory that contains libraries packaged in JAR files. The library-directory element of the .ear file’s deployment descriptor contains the name of this directory. If a library-directory element isn’t specified, or if the .ear file does not contain a deployment descriptor, the directory named lib is used. An empty library-directory element may be used to specify that there is no library directory. All files in this directory (but not subdirectories) with a .jar extension must be made available to all components packaged in the EAR file, including application clients. These libraries may reference other libraries, either bundled with the application or installed separately, using any of the techniques described herein.

    This does not mean that method B is incorrect, it is the one to be used for application servers like JBoss 4, that did not support the library-directory element in application.xml. I believe, Glassfish also supports the lib directory concept without a corresponding library-directory element.

    Coming back to the question, placing classes alone in a directory in the EAR file appears to be supported only in WebLogic Server via the APP-INF\classes structure (needless to say, this is not a platform standard). Hence, it is recommended to jar the common classes and use the application server supported mechanism to make these common classes available to other modules in the application.