File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes Jar file in jar file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Jar file in jar file" Watch "Jar file in jar file" New topic

Jar file in jar file

Tim Manchester
Ranch Hand

Joined: Nov 03, 2004
Posts: 62

I have a simple java class with a main method that I intend to call in the following manner:
java -jar helloWorld.jar

In the helloWorld class, I intend to do a little logging, so I include the log4j-1.2.8.jar in my project. It works great in the IDE (WSAD 5.1) but no so much when I creat the jar file and try to execute it from the command line.

I know that the problem is related to the log4j jar file. I rolled it into the helloWorld.jar and included it in the manifest file, but I still get the NoClassDefFound error. If I put the log4j jar file in the same directory as my jar, the command line works fine.

So my question really is: How do I write my manifest file to tell the app that the jar it's looking for is in another jar? Or is this even possible? I'd rather keep things bundled up into one file if at all possible.


- Tim
Geoffrey Falk
Ranch Hand

Joined: Aug 17, 2001
Posts: 171
The trick is getting the log4j classes where the jar classloader can find them. Probably the simplest thing to do is build a jar that contains the union of your classes and the log4j classes (and any other libs you use).

If you use Ant, you could do it using something like:


Sun Certified Programmer for the Java 2 Platform
subject: Jar file in jar file
jQuery in Action, 3rd edition