2

I have got a JAR file which contains Utility classes, that are used in my Web Application.

Now some of the Utility classes require their respective libraries such as commons-logging, db2java.

But my final Utility JAR being packaged does not contain these required libraries , I tried including them but they are not picked up by the classes.

Can anybody tell me as to how can I include these dependant libraries inside my Utility JAR in such a way that my classes pick them and execute properly.

Vivek
  • 2,091
  • 11
  • 46
  • 61
  • How are you building your war file? – Paul Dec 14 '11 at 16:07
  • possible duplicate of [How to merge module jars to a single jar in Maven2?](http://stackoverflow.com/questions/1895537/how-to-merge-module-jars-to-a-single-jar-in-maven2) – Kris Dec 14 '11 at 16:09
  • @Kris, if he's building a webapp then this question is not a dupe of the one you referenced. – Paul Dec 14 '11 at 16:11
  • 1
    @Kris. I'm not a big fan of an "uber" jar. If Vivek was to package commons-logging into a single JAR and anther developer comes along an puts a different version of commons-logging into the application you can get unpredictable results that are not obvious – Brad Dec 14 '11 at 16:12
  • I am building my WAR file using ANT , and this Utility JAR is part of the WEB-INF folder The issue is with the classes present inside the Utility JAR, since I am unable to understand as to how can I reference the required libraries of the Utility classes inside my JAR. i guess creating a JAR of JAR's is an issue right ?? – Vivek Dec 14 '11 at 16:15
  • @Vivek, see Brad's answer...he's correct. A jar of jars won't work in a webapp anyway...besides, it's unnecessary and will cause you maintenance headaches down the road. If you're having trouble with your Ant task, open a new question and post your war target. – Paul Dec 14 '11 at 16:19

2 Answers2

5

Just put all the lib's in your WEB-INF/lib folder and not try to bundle them into one JAR file. Having your libs broken out into their own JAR files is easier to manage.

WEB-INF/lib/
....commons-lang-3.0.jar
....db2java-1.0.jar
....my-utilities-1.0.jar

You can also see the versions of the jars (in case you want to update one) without having to go digging around for them

Brad
  • 15,186
  • 11
  • 60
  • 74
  • Actually, a jar file within a jar file is not just harder to manage. It's *impossible* to manage for the web container. – JB Nizet Dec 14 '11 at 16:11
  • But in this case , my Utility classes are present inside my Utility JAR. If i place the required libs in the WEB-INF/lib folder , how would my Utility classes be able to access them ? I mean , would they be picked up ? – Vivek Dec 14 '11 at 16:11
  • What he said. Don't bundle everything together - it may seem like a time saver but eventually it will make you pay... – Paul Dec 14 '11 at 16:12
  • 2
    @Vivek, yes. The application container (e.g. Tomcat, Jetty) will add the jars in WEB-INF/lib to the app's classpath. – Paul Dec 14 '11 at 16:13
2

If you are interested in a way to store all of the dependent jar files inside of a single jar, I would recommend using the fatjar plugin. It does exactly what you are asking for. I've used it many times for quick deployment with web applets, and desktop apps.

Jyro117
  • 4,519
  • 24
  • 29
  • Thanks for the tip , i'll keep that in mind. However Brad's suggestion worked out for me – Vivek Dec 14 '11 at 18:18