Question

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!

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top