0

I'm using ANT for creating a JAR file of my application. As I read, I should put the external Jars I need as arguments from the command line, but I want to build a Manifest file.

This is my build.xml file (only the part in which I build the jar)

<!-- Build jar from classes -->
    <target name="jar" depends="compile" description="Build a jar file.">
         <manifestclasspath property="jar.classpath" jarfile="${target.build.main}/main">
             <classpath>
                 <fileset dir="${target.build.main}/lib" includes="*.jar"/>
             </classpath>
         </manifestclasspath>
<jar destfile="${target.dist}/configuration-deployer.jar" basedir="${target.build.main}">
        <manifest>
            <attribute name="Main-Class" value="${dist.main.class}" />
            <attribute name="Class-Path" value="${jar.classpath}" />
        </manifest>
    </jar>
</target>

Now this is an extract of the manifest file

Manifest-Version: 1.0
Ant-Version: Apache Ant 1.8.4
Created-By: 1.6.0_45-b06 (Sun Microsystems Inc.)
Main-Class: com.sap.coas.deployer.StoreInstanceOnMove
Class-Path: lib/ConfigurationMetamodel-1.jar lib/Consumer_api-1.jar li
b/FunctionalSystemMetaModel-1.jar lib/WS_api-1.jar lib/activemq-core-
5.7.0.jar lib/activemq-protobuf-1.1.jar ..... other jar

If I open the JAR generated, I can see the manifest inside META-INF folder, and the lib folder containes all the libraries. So the structure of the JAR is this one:

/META-INF/manifest.mf
/lib/ConfigurationMetamodel-1.jar
/lib/otherJar.jar ........

But when I try to run

java -jar myJar.jar

I got the ClassNotFoundException, and I'm sure that the jar containing the class that throw the exception is in the lib folder.

Thanks!

Alessio
  • 2,018
  • 25
  • 26
  • Try including all the jar files on the command line by using -classpath or -cp switch: java -jar myJar.jar -cp lib/a.jar;lib/b.jar and so on... – Manish Oct 10 '13 at 08:15
  • But did you make sure that the lib folder is situated in the same folder as that of the runnable jar? – Ankur Shanbhag Oct 10 '13 at 08:16
  • @Manish it will take too much time because I have to distribute this JAR. Actually, I've found (I think) the solution: the property jarfile of manifestclasspath was wrong, putting ${target.dist}/configuration-deployer.jar is working now, any ideas of the reason? – Alessio Oct 10 '13 at 08:20
  • @AlessioCavaleri ant will use the jarfile property to calculate the relative path for the libraries. The file given in the jarfile attribute to the elements of the nested classpath must be the same as you expect them to be when deploying the jar. – longhua Oct 10 '13 at 08:58
  • ok, now I can see that in the manifest the path used in like "../build/main/lib/externalJAr/jar" and it's working. but I should want to use the jars INSIDE /lib folder into my jar. it's possible? – Alessio Oct 10 '13 at 09:38
  • found the real solution here http://stackoverflow.com/questions/1821803/creating-a-bundle-jar-with-ant – Alessio Oct 10 '13 at 12:31

1 Answers1

0

You couldn't use Class-Path header in manifest file to point to the JAR's in the JAR file. Please check the following link. You should place the lib folder together with those JAR's in the same folder of myJar.jar.

Adding Classes to the JAR File's Classpath

Note: The Class-Path header points to classes or JAR files on the local network, not JAR files within the JAR file or classes accessible over internet protocols. To load classes in JAR files within a JAR file into the class path, you must write custom code to load those classes. For example, if MyJar.jar contains another JAR file called MyUtils.jar, you cannot use the Class-Path header in MyJar.jar's manifest to load classes in MyUtils.jar into the class path.

longhua
  • 4,142
  • 21
  • 28