简体   繁体   中英

Java ANT : Issues with build.xml

I was wondering if someone could help me with a serious issue... I need to create a jar from a project using a lot of dependencies... Problem is : my build.xml file I wrote with my own hand is not working. More specifically it runs without any problem, just a warning :

Buildfile: C:\Users\Alexis Fasquel\workspace\FirstEditServer\build.xml
clean:
   [delete] Deleting directory C:\Users\Alexis Fasquel\workspace\FirstEditServer\bin
   [delete] Deleting directory C:\Users\Alexis Fasquel\workspace\FirstEditServer\dist
makedir:
    [mkdir] Created dir: C:\Users\Alexis Fasquel\workspace\FirstEditServer\bin
    [mkdir] Created dir: C:\Users\Alexis Fasquel\workspace\FirstEditServer\dist
compile:
    [javac] C:\Users\Alexis Fasquel\workspace\FirstEditServer\build.xml:101: warning: 'includeantruntime' was not set, defaulting to build.sysclasspath=last; set to false for repeatable builds
    [javac] Compiling 14 source files to C:\Users\Alexis Fasquel\workspace\FirstEditServer\bin
jar:
      [jar] Building jar: C:\Users\Alexis Fasquel\workspace\FirstEditServer\dist\firstEditServer.jar
main:
BUILD SUCCESSFUL
Total time: 1 second

But unfortunately I still get some classNotFoundDefError


I didn't print the full trace because it's the first class that i tried to instanciate which is not found... So i'm guessing nothing's working... but here it is :

Exception in thread "main" java.lang.NoClassDefFoundError: gate/util/GateExcept
on
        at java.lang.Class.getDeclaredMethods0(Native Method)
        at java.lang.Class.privateGetDeclaredMethods(Unknown Source)
        at java.lang.Class.getMethod0(Unknown Source)
        at java.lang.Class.getMethod(Unknown Source)
        at sun.launcher.LauncherHelper.getMainMethod(Unknown Source)
        at sun.launcher.LauncherHelper.checkAndLoadMain(Unknown Source)
Caused by: java.lang.ClassNotFoundException: gate.util.GateException
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        ... 6 more

There is my build.xml file... Could someone take a look at it, and maybe find what's my error ?

<?xml version="1.0"?>
<project name="Ant-Test" default="main" basedir=".">
    <!-- Sets variables which can later be used. -->
    <!-- The value of a property is accessed via ${} -->
    <property name="src.dir" location="src" />
    <property name="bin.dir" location="bin" />
    <property name="gate.dir" location="gate" />
    <property name="virtuoso.dir" location="virtuoso" />
    <property name="recommendation.dir" location="recommendation" />
    <property name="dist.dir" location="dist" />

    <path id="bin.classpath">
        <fileset dir="${virtuoso.dir}">
            <include name="openrdf-sesame-2.6.5-onejar.jar" />
            <include name="slf4j-api-1.5.0.jar" />
            <include name="slf4j-jdk14-1.5.0.jar" />
            <include name="virt_sesame2.jar" />
            <include name="virtjdbc3.jar" />
        </fileset>

        <fileset dir="${gate.dir}">
            <include name="plugins\Gazetteer_LKB\Gazetteer_LKB.jar" />
            <include name="lib\gate.jar" />
            <include name="lib\log4j.jar" />
            <include name="lib\commons-io.jar" />
            <include name="plugins\Tagger_Framework\TaggerFramework.jar" />
            <include name="lib\ant-contrib.jar" />
            <include name="lib\ant-launcher.jar" />
            <include name="lib\ant.jar" />
            <include name="lib\aopalliance.jar" />
            <include name="lib\apache-mime4j-core.jar" />
            <include name="lib\apache-mime4j-dom.jar" />
            <include name="lib\bcmail-jdk15.jar" />
            <include name="lib\bcprov-jdk15.jar" />
            <include name="lib\commons-codec.jar" />
            <include name="lib\commons-compress.jar" />
            <include name="lib\commons-lang.jar" />
            <include name="lib\commons-logging.jar" />
            <include name="lib\dom4j.jar" />
            <include name="lib\fontbox.jar" />
            <include name="lib\gate-asm.jar" />
            <include name="lib\gate-compiler-jdt.jar" />
            <include name="lib\lucene-core.jar" />
            <include name="lib\metadata-extractor.jar" />
            <include name="lib\nekohtml.jar" />
            <include name="lib\pdfbox.jar" />
            <include name="lib\poi-ooxml-schemas.jar" />
            <include name="lib\poi-ooxml.jar" />
            <include name="lib\poi-scratchpad.jar" />
            <include name="lib\poi.jar" />
            <include name="lib\spring-aop.jar" />
            <include name="lib\spring-beans.jar" />
            <include name="lib\spring-core.jar" />
            <include name="lib\tika-core.jar" />
            <include name="lib\tika-parsers.jar" />
            <include name="lib\wstx-lgpl.jar" />
            <include name="lib\xercesImpl.jar" />
            <include name="lib\xmlbeans.jar" />
            <include name="lib\xmlunit.jar" />
            <include name="lib\xpp3.jar" />
            <include name="lib\xstream.jar" />
            <include name="lib\ivy.jar" />
            <include name="lib\java-getopt.jar" />
            <include name="lib\java2html.jar" />
            <include name="lib\javacc.jar" />
            <include name="lib\jaxen.jar" />
            <include name="lib\jdom.jar" />
            <include name="lib\jempbox.jar" />
            <include name="lib\junit.jar" />
            <include name="plugins\Gazetteer_LKB\lib\commons-cli-1.1.jar" />
            <include name="plugins\Gazetteer_LKB\lib\commons-collections-3.2.1.jar" />
            <include name="plugins\Gazetteer_LKB\lib\commons-httpclient-3.1.jar" />
            <include name="plugins\Gazetteer_LKB\lib\kim-api-3.0-RC5.jar" />
            <include name="plugins\Gazetteer_LKB\lib\kim-util-3.0-RC5.jar" />
            <include name="plugins\Gazetteer_LKB\lib\owlim-lite-4.3.jar" />
            <include name="plugins\Gazetteer_LKB\lib\trove4j-2.0.2.jar" />
            <include name="plugins\Gazetteer_LKB\lib\openrdf-sesame-2.6.3-onejar.jar" />
            <include name="plugins\Gazetteer_LKB\lib\slf4j-api-1.5.11.jar" />
            <include name="plugins\Gazetteer_LKB\lib\slf4j-log4j12-1.5.11.jar" />
        </fileset>

        <fileset dir="${recommendation.dir}">
            <include name="simmetrics_jar_v1_6_2_d07_02_07.jar" />
        </fileset>
    </path>

    <!-- Deletes the existing bin, docs and dist directory-->
    <target name="clean">
        <delete dir="${bin.dir}" />
        <delete dir="${dist.dir}" />
    </target>

    <!-- Creates the  bin and dist directory-->
    <target name="makedir">
        <mkdir dir="${bin.dir}" />
        <mkdir dir="${dist.dir}" />
    </target>

    <!-- Compiles the java code (including the usage of library for JUnit -->
    <target name="compile" depends="clean, makedir">
        <javac srcdir="${src.dir}" destdir="${bin.dir}" classpathref="bin.classpath">
        </javac>

    </target>


    <!--Creates the deployable jar file  -->
    <target name="jar" depends="compile">
        <jar destfile="${dist.dir}\firstEditServer.jar" basedir="${bin.dir}">
            <manifest>
                <attribute name="Main-Class" value="com.firstedit.main.Main" />
            </manifest>
        </jar>
    </target>

    <target name="main" depends="compile, jar">
        <description>Main target</description>
    </target>

</project>

As you can see, GateException is really in the gate.jar...

在此处输入图片说明

Assuming just for a moment that the class it isn't finding is in the jar file lib\\gate.jar, I would say that what is happening is you are constructing the jar (firstEditServer.jar) file from the source of your project and that is all. In order to execute that jar, you would also need to ensure all of the jar files in your lib... directory are also in the classpath.

Now, if the intention is that you want a single jar file constructed that will include all classes in the library jars then you have to build your jar file slightly differently. You can't build an executable jar (one with a Main-Class attribute) that includes other jars. The standard classloader will not look inside one jar for other jar files. So you need to expand the library jars classes into your final jar.

Ant provides the support for this using something like the below. The zipgroupfileset is a task that will take a list of jar files, expand them and include the classes themselves into the target jar. I filter out manifest.mf and properties files so they do not collide with my own (such as a log4j.properties file in a library jar).

This creates a single jar with all contents of all lib jars. Manifest files will collide and the last manifest file seen wins.

<jar destfile='${build.dir}/lib-jars.jar'>
   <zipgroupfileset dir='${lib.dir}'>
     <include name='**/*.jar'/>
   </zipgroupfileset>
</jar>

The zipfileset used here with a single jar can filter out any contents from the one jar desired. This removes the MANIFEST.MF file, but could also remove other files with additional exclude tags.

<jar destfile='${jar.file}' basedir='${classes.dir}'>
  <zipfileset src='${build.dir}/lib-jars.jar'>
    <exclude name="META-INF/MANIFEST.MF"/>
  </zipfileset>
  <manifest>
    <attribute name="Built-By" value="${user.name}"/>
    <attribute name="Main-Class" value="${main.class}"/>
    <section name="common">
      <attribute name="Specification-Title" value="${project.title}"/>
      <attribute name="Specification-Version" value="${release.version}"/>
      <attribute name="Specification-Vendor" value="${vendor}"/>
      <attribute name="Implementation-Title" value="${project.title}"/>
      <attribute name="Implementation-Version" value="${release.version} ${TODAY}"/> 
      <attribute name="Implementation-Vendor" value="${vendor}"/>
    </section>
  </manifest>      
</jar>

Make sure your lib folder is in your classpath. I have something similar i did myself like you are. The build is okay because ant finds the jars. If you are building a jar, make sure your manifest adds the jars to your classpath in your manifest file. There's a manifest option and you can have ant add a manifest file to your jar. If nothing else run your program on command line and use the -classpath option to point to your lib folder. I think your issue is around your classpath though, I've fought this battle many times.

This is not an answer, but it's too long for comment, so...

You can these steps:

  1. add simple main method to gate.util.GateExcept
public static void main( final String[] args ) {
    System.out.println( GateExcept.class.getName() );
}
  1. build the jar again (let it call myjar.jar)

  2. run this command

    java -cp myjar.jar gate.util.GateExcept

  3. if that works fine try (executable jar)

    java -jar myjar.jar

It seems to me that on screenshot you have gate.util.GateException , but in exception stack there is only gate.util.GateExcep (but probably this is just SO problem).

Let me know if that works.

Correct me if I'm wrong, but you're not running this jar using ANT are you? ANT is only used to build the jar file correct?

There are two ways to launch your java program

  1. java -jar myapp.jar
  2. java -cp myapp.jar MyAppClass

The first requires you to set the "Main-Class" and "Class-Path" manifest entries. The second requires you to list each dependency explicitly.

I suspect you are trying the former, but forgetting to include the classpath..... The following question had the same problem. It outlines a solution that uses the ANT manifestclasspath task to simplify the classpath setting in your manifest:

Cannot find Main Class in File Compiled With Ant

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM