Including jibernate jar dependencies in ant build

I am trying to compile a runnable jar file for a project that uses hibernate. I'm trying to build an ant build.xml file to simplify my build process, but I'm having trouble including the hibernate3.jar inside the final jar file.

If I run the ant script, I manage to include all my libraries and they are placed in the final root of the jar file. When I run the jar file I get

java.lang.NoClassDefFoundError: org/hibernate/Session 

      

error. If I use the built-in jar export in Eclipse, it only works if I select "extract required libraries to jar". But this bloats the bank and involves too much of my project (i.e. Unit tests).

Below is my generated manifest:

Manifest-Version: 1.0
Main-Class: main.ServerImpl
Class-Path: ./ antlr-2.7.6.jar commons-collections-3.1.jar dom4j-1.6.1.jar
hibernate3.jar javassist-3.9.0.GA.jar jta-1.1.jar slf4j-api-1.5.11.jar 
slf4j-simple-1.5.11.jar mysql-connector-java-5.1.12-bin.jar rmiio-2.0.2.jar 
commons-logging-1.1.1.jar

      

And part of the build.xml file looks like this:

<target name="dist" depends="compile" description="Generates the Distribution Jar(s)">
    <mkdir dir="${dist.dir}" />
    <jar destfile="${dist.dir}/${dist.file.name}.jar" basedir="${build.prod.dir}" filesetmanifest="mergewithoutmain">
        <manifest>
            <attribute name="Main-Class" value="${main.class}" />
            <attribute name="Class-Path" value="./ ${manifest.classpath} " />
            <attribute name="Implementation-Title" value="${app.name}" />
            <attribute name="Implementation-Version" value="${app.version}" />
            <attribute name="Implementation-Vendor" value="${app.vendor}" />
        </manifest>
        <zipfileset refid="hibernatefiles" />
        <zipfileset refid="slf4jfiles" />
        <zipfileset refid="mysqlfiles" />
        <zipfileset refid="commonsloggingfiles" />
        <zipfileset refid="rmiiofiles" />
    </jar>
</target>

      

The "refids" for zipfilesets point to directories in the lib directory at the root of the project. The manifest.classpath variable takes the classpath of all these library jars and aligns them with pathconvert and mapper.

I also tried setting the manifest class path to ".", "./" and only the library jar, but no difference at all. I hope there are simple remedies to solve my problems ...

+2


a source to share


1 answer


Since you cannot specify jar-inside-jar in your classpath, this way will not work. You need to either compress just your code and then pin the jar with the libs banners and run a script like

#!/bin/sh
java -cp ./hibernate.jar:./mycode.jar... my.Main

      



(this is what most people do) or you can unpack all your libs into the same directory that contains your compiled classes and then the jar result dir back into one jar file (this is what the maven jar-with packaging does -dependencies).

+2


a source







All Articles