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 Jars within Jar Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Jars within Jar" Watch "Jars within Jar" New topic

Jars within Jar

Hasnain Javed Khan
Ranch Hand

Joined: Nov 23, 2007
Posts: 73
Hello All,
I have written a custom ant task and have made a jar and placed it in Ant's lib directory. The jar file has some dependencies which are jar files them selves (jdom.jar,commons-lang.jar and javagit.jar to be exact). It also has a dependency on ant.jar but I have not included that in the application's jar file.When I execute the custom ant task, I get an error "ClassNotFoundException" for classes that are being used in the Custom Ant Task class (except for Classes in ant.jar).When I extract the dependencies and then create a jar file (no jars within the jar file), it works fine !

Here is the METAINF file when the dependencies are extracted
Manifest-Version: 1.0

Ant-Version: Apache Ant 1.7.1

Created-By: 11.0-b15 (Sun Microsystems Inc.)

Class-Path: . (in case of jars --> jdom.jar javagit.jar commons-lang.jar)

1. How come I am not getting any errors for classes in ant.jar ?
2. Can any one kindly explain why the JRE can't find the files from with in the dependencies as jar files as opposed to dependencies as extracted files ?
3. Is there a way to create a jar file with dependencies as jar files and how to configure it so that the runtime can find the files from within the jar files ?

Waiting for a favorable response.

Kind Regards.
Moojid Hamid
Ranch Hand

Joined: Mar 07, 2009
Posts: 120
Welcome to JavaRanch.

1- since ant.jar is nested it works.

2- Nested Jars are not loaded by default class loader. i.e. nested jar files are not supported.

3- There are some other tools (try google) that make it possible, but by default it does not work. If you want a single Jar, one option is to unjar all the jar files and create a single jar from the class files.
Hasnain Javed Khan
Ranch Hand

Joined: Nov 23, 2007
Posts: 73
Thank you so much for your kind response.
I'll search it on google.
Kind Regards.
I agree. Here's the link:
subject: Jars within Jar
It's not a secret anymore!