views:

223

answers:

2

I tried to create a jar file from a java project, which uses some external jars. I created a lib folder and put all the jars I need there.

I run the project in eclipse by adding all the jars in the lib folder to the Build Path and it works ok.

When I try to create the jar with ant from build.xml, it seems ok, no error is shown.

When I try to run the jar, I get the message "Invalid or corupt jarfile".

In build.xml: I define the path to use for compiling:

<path id="project.classpath">  
     <fileset dir="${lib}">
         <include name="**/*.jar"/>
     </fileset>
</path>

This is the target for the compilation:

<target name="compile" depends="init" description="compile the source " >
    <!-- Compile the java code from ${src} into ${build} -->
    <javac srcdir="${src}" destdir="${build}">  
          <classpath refid="project.classpath"/>  
    </javac>  
</target>

And this is the target for making the jar file:

<target name="dist" depends="compile" description="generate the distribution" >
      <mkdir dir="${dist}"/>
      <!-- Put everything in ${build} into the MyProject-${DSTAMP}.jar file -->
      <jar jarfile="${dist}/MyProject-${DSTAMP}.jar" basedir="${build}"> 
          <manifest>
           <attribute name="Main-Class" value="${main}" />
          <attribute name="Class-Path:" value="lib/**/*.*"/>
          </manifest>
          <fileset dir="${src}" includes="images/**/*.*" />
       </jar>
       <echo file="${dist}/start.bat" message="java -jar MyProject-${DSTAMP}.jar" />
  </target>

Can you please tell me what have I done wrong?

A: 

I don't think your Class-Path attribute should have a trailing colon specified in your build.xml.

Try using

jar tvf {jarname}

from the command line, and see if that can expand your jar file, and whether it contains what you expect (the above will simply dump the table of contents, but is a useful check)

EDIT: Changed to reflect the feedback below

Brian Agnew
I also tried this version:<attribute name="Class-Path:" value="lib/**/*.jar"/> And still didn't work
Jenny Smith
If you look in your manifest file, you'll see that written in. But are the classes actually in your .jar file ?
Brian Agnew
I opened the jar and it seemed to include everyting. At least all the classes and files in the project.Manifest-Version: 1.0Ant-Version: Apache Ant 1.7.0Created-By: 11.0-b15 (Sun Microsystems Inc.)Main-Class: controller.ClientClass-Path:: lib/**/*.jar
Jenny Smith
Yes, the classes are in the jars included.
Jenny Smith
Note the double colon in Class-Path
Brian Agnew
If I don't inlude the Class Path in the manifest file, then I get the exception NoClassDeffFoundError
Jenny Smith
Brian, you were right. The colod was the problem. Now I get the NoClassDefFoundError. I search the jar for that class and it's there. I also use some other classes in the application (before the one which generates the problem) from the same jar and I get no exception.Do you have any ideea why is this happening?
Jenny Smith
You may want to check that classes referenced IN the class that you're getting the NoClassDefFound error are also there. Otherwise the class can't be loaded/constructed properly.
Brian Agnew
They are there. By the way, is there a problem if I use threads in those classes?
Jenny Smith
+3  A: 

First remove the colon after Class-Path: to match

<attribute name="Class-Path" value="lib/**/*.*"/>

Then I suggest reading

HOWTO Create MANIFEST.MF Classpath From Ant or better using Manifestclasspath

jitter
It worked with the manifestclasspath. The problem was indeed that the jar names were way too long than the 72 characters allowed.
Jenny Smith
Lines in MANIFEST.MF are wrappable. I too think the culprit is the extra colon.
Thorbjørn Ravn Andersen
+1 for the Manifestclasspath. Haven't looked at the new goodies since 1.6.2, so this is a nice addition.
Thorbjørn Ravn Andersen