tags:

views:

27

answers:

2

I have written an ANT script and finally am building the jar

here is the building of jar section

 <jar jarfile="${destination}/@{name}.jar">
            <fileset dir="${output}">
                <include name="abc/xyz/@{name}/**"/>
            </fileset>
            <zipfileset dir="lib" prefix="lib/"/>
            <manifest>
                <attribute name="Main-Class" value="com.abc.xyz.HelloWorld"/>
                <attribute name="Class-Path" value=".:lib/activation.jar:lib/antlr-2.7.6.jar:lib/asm-attrs.jar:lib/asm.jar:lib/cglib-2.1.3.jar:lib/commons-collections-2.1.1.jar:lib/commons-logging-1.1.jar:lib/dom4j-1.6.1.jar:lib/ehcache-1.2.3.jar:lib/ejb3-persistence.jar:lib/hibernate-annotations.jar:lib/hibernate-commons-annotations.jar:lib/hibernate-entitymanager.jar:lib/hibernate-tools.jar:lib/hibernate3.jar:lib/javassist.jar:lib/jdbc2_0-stdext.jar:lib/jta.jar:lib/mysql-connector-java-5.1.5-bin.jar"/>
                </manifest>
        </jar>

Now when I try to execute the package it executes, but whenever data is sent I get an error.

java.lang.NoClassDefFoundError: javax/persistence/NoResultException

But the persistence jar is there in the class-path, I've tried all the combinations for the class-path, but in vain.

But in another system I tried without create a jar like

set classpath=.;lib/activation.jar;lib/antlr-2.7.6.jar;lib/asm-attrs.jar;lib/asm.jar;lib/cglib-2.1.3.jar;lib/commons-collections-2.1.1.jar;lib/commons-logging-1.1.jar;lib/dom4j-1.6.1.jar;lib/ehcache-1.2.3.jar;lib/ejb3-persistence.jar;lib/hibernate-annotations.jar;lib/hibernate-commons-annotations.jar;lib/hibernate-entitymanager.jar;lib/hibernate-tools.jar;lib/hibernate3.jar;lib/javassist.jar;lib/jdbc2_0-stdext.jar;lib/jta.jar;lib/mysql-connector-java-5.1.5-bin.jar
java com.abc.xyz.HelloWorld

This works fine.

In Mac when I try like this:

java -cp .:lib/activation.jar:lib/antlr-2.7.6.jar:lib/asm-attrs.jar:lib/asm.jar:lib/cglib-2.1.3.jar:lib/commons-collections-2.1.1.jar:lib/commons-logging-1.1.jar:lib/dom4j-1.6.1.jar:lib/ehcache-1.2.3.jar:lib/ejb3-persistence.jar:lib/hibernate-annotations.jar:lib/hibernate-commons-annotations.jar:lib/hibernate-entitymanager.jar:lib/hibernate-tools.jar:lib/hibernate3.jar:lib/javassist.jar:lib/jdbc2_0-stdext.jar:lib/jta.jar:lib/mysql-connector-java-5.1.5-bin.jar com.abc.xyz.HelloWorld

Also it works fine :(, but the minute I create the jar it stops.

How can I resolve this issue?

A: 

First of all the "Main-Class" and "Class-Path" manifiest entries is only used for executeable jars. In other words when you invoke java as follows:

java -jar foo.jar

Invoking java using the -cp option means you're supplying your own classpath and note that you also have to provide the main class on the command line as well.

Secondly you need to replace the ":" characters with spaces:

<attribute name="Class-Path" value=". lib/activation.jar lib/antlr-2.7.6.jar lib/asm-attrs.jar lib/asm.jar lib/cglib-2.1.3.jar lib/commons-collections-2.1.1.jar lib/commons-logging-1.1.jar lib/dom4j-1.6.1.jar lib/ehcache-1.2.3.jar lib/ejb3-persistence.jar lib/hibernate-annotations.jar lib/hibernate-commons-annotations.jar lib/hibernate-entitymanager.jar lib/hibernate-tools.jar lib/hibernate3.jar lib/javassist.jar lib/jdbc2_0-stdext.jar lib/jta.jar lib/mysql-connector-java-5.1.5-bin.jar"/>

Finally I'd recommend using the manifestclasspath task to build your classpath string for you. It will correcly resolve any relative links between your jar and it's run-time dependencies.

<manifestclasspath property="mf.classpath" jarfile="${destination}/@{name}.jar">
    <classpath>
        <fileset dir="lib" includes="*.jar"/>
    <classpath>
</manifestclasspath>

<jar jarfile="${destination}/@{name}.jar">
    <fileset dir="${output}">
       <include name="abc/xyz/@{name}/**"/>
    </fileset>
    <zipfileset dir="lib" prefix="lib/"/>
    <manifest>
       <attribute name="Main-Class" value="com.abc.xyz.HelloWorld"/>
       <attribute name="Class-Path" value=". ${mf.classpath}"/>
    </manifest>
</jar>

One final observation.... Why are you including the contents of the lib directory inside the jar? (The zipfileset tag in the jar command?)

This appears unnecessary, all you need to do is ensure that the run-time dependencies are present in a lib directory as specified in your Class-Path manifest entry.

Mark O'Connor
A: 

The reason for it not works was there not in the class path, the explanation is given here: Java-Jar-Ignores-Classpath-Workaround

Panther24