tags:

views:

340

answers:

3

We have a core library that we include as a JAR with all of our web applications to maximize code reuse. Now we'd like to include some functionality through this core library via a 3rd party library (iText).

Adding the iText JAR to the core library doesn't work because nested JARs aren't found by the classloader. We don't want to add the iText JAR to each Web Application because the web applications aren't using iText, They are using a library that is using iText. Semantics? Perhaps, but adding third party JARs to a Web App so a library it uses can access the classes in the JAR seems messy.

Am I missing a simple practice that addresses this or are jar-merging and custom class loaders the best approaches?

+1  A: 

An EAR would probably be the appropriate thing for this. Nothing like another layer of indirection.

krosenvold
+2  A: 

Welcome to JAR hell, an ongoing issue in Java development, and one of the main reasons that OSGi is gaining steam. "Messy" approaches have been standard in the industry until only very recently, and the costs of clean approaches may well outweigh the cost of the quick and dirty approach.

But to answer your question, most app servers have a place where you can put JARS that are accessible across all web apps. Tomcat, for instance, has the common/lib directory. You probably want to put your JAR there.

rtperson
+1  A: 

If you distribute wars, you will have to place iText jar inside the war.

If you distribute ears, you can place all jars common to multiple wars inside ear root.

If jar is common to many ears, there is an option to place it inside web server library dir. This final option is not recommendable in my opinion, since you might use different versions of jar in each ear, thus producing conflict, and placing jar inside server root is generally not standard distribution mechanism.

There are tools that will repack all of your libraries and embed them into the single jar like Jar Jar Links.
Finally, I recommend using Maven or similar tool to manage your dependencies and automate your builds. It can be of great help as dependencies start to grow in number.

Dan