Use library-directory in JavaEE 5 apps

Starting from JavaEE 5, you can specify a directory inside the an EAR file to hold jar files that can be used by all modules of that EAR. For example, if you've built an EAR called hello.ear:

The content of META-INF/application.xml:
<?xml version="1.0" encoding="UTF-8"?>
<application xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="5"



All classes and resources in all jar files under shared directory (e.g., util-1.jar, util-2.jar) are accessible to hello-ejb.jar and hello.war. For instance, you may choose to put hello-ejb's business interfaces, component interfaces and home interfaces in util-1.jar, to avoid duplicates in ejb jar and war.

Better yet, you don't even need to have an application.xml to use this mechanism. Recall that all descriptors are optional in JavaEE 5. The default library-directory is a directory named lib inside the EAR. You just put your shared jar files here and they will be automatically made accessible for all components. However, these files do need to use *.jar extension.


umer said...

thanks for the info

Venkat said...

Nice one. How to achieve shared library concept in previous versions of J2EE?

javahowto said...

Q: How to achieve shared library concept in previous versions of J2EE?

A: Use Class-Path entries in MANIFEST.MF in your WAR, ejb-jar, or application client jar files. For example:

Manifest-Version: 1.0
Ant-Version: Apache Ant 1.6.5
Created-By: 1.5.0_06-b05 (Sun Microsystems Inc.)
Class-Path: commons-foo.jar

This is the layout of the EAR file:


Ant task jar or war has attributes and subelements that let you customize MANIFEST.MF. So usually no need to hand craft one.

shared folder protect said...

It really helps me. thank you.

Steve Smith said...

Great and Useful Article.

Online Java Training

Java Online Training India

Java Online Course

Java EE course

Java EE training

Best Recommended books for Spring framework

Java Interview Questions

Java Course in Chennai

Java Online Training India