简体   繁体   English

Java ANT:build.xml问题

[英]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. 我想知道是否有人可以帮助我解决一个严重的问题……我需要使用很多依赖项从一个项目中创建一个jar……问题是:我自己编写的build.xml文件无法正常工作。 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 但是不幸的是我仍然得到一些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 ? 有我的build.xml文件...有人可以看一下,也许发现我的错误是什么?

<?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... 如您所见,GateException确实位于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. 暂时假设它找不到的类位于jar文件lib \\ gate.jar中,那么我想说的是,您正在从项目源中构造jar(firstEditServer.jar)文件,并且就这些。 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. 为了执行该jar,您还需要确保lib ...目录中的所有jar文件也位于类路径中。

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. 现在,如果要创建一个包含所有jar库中所有类的 jar文件则必须以稍有不同的方式构建jar文件。 You can't build an executable jar (one with a Main-Class attribute) that includes other jars. 您无法构建包含其他jar的可执行jar(带有Main-Class属性的jar)。 The standard classloader will not look inside one jar for other jar files. 标准类加载器不会在一个jar中查找其他jar文件。 So you need to expand the library jars classes into your final jar. 因此,您需要将库jar类扩展最终的jar中。

Ant provides the support for this using something like the below. Ant使用如下所示的方式对此提供了支持。 The zipgroupfileset is a task that will take a list of jar files, expand them and include the classes themselves into the target jar. zipgroupfileset是一项任务,它将获取jar文件列表,将其展开并将类本身包含到目标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). 我过滤掉manifest.mf和属性文件,以免它们与我自己的文件发生冲突(例如库jar中的log4j.properties文件)。

This creates a single jar with all contents of all lib jars. 这将创建一个包含所有lib jar的所有内容的jar。 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. 与单个jar一起使用的zipfileset可以从所需的一个jar中过滤掉所有内容。 This removes the MANIFEST.MF file, but could also remove other files with additional exclude tags. 这将删除MANIFEST.MF文件,但也可能会删除带有其他排除标记的其他文件。

<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. 确保您的lib文件夹在您的类路径中。 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. 如果要构建jar,请确保清单将jar添加到清单文件中的类路径中。 There's a manifest option and you can have ant add a manifest file to your jar. 有一个清单选项,您可以让ant将清单文件添加到jar中。 If nothing else run your program on command line and use the -classpath option to point to your lib folder. 如果没有其他要求,请在命令行上运行程序,然后使用-classpath选项指向您的lib文件夹。 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 gate.util.GateExcept添加简单的main方法
public static void main( final String[] args ) {
    System.out.println( GateExcept.class.getName() );
}
  1. build the jar again (let it call myjar.jar) 再次构建jar(将其命名为myjar.jar)

  2. run this command 运行此命令

    java -cp myjar.jar gate.util.GateExcept

  3. if that works fine try (executable jar) 如果工作正常,请尝试(可执行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). 在我看来,在屏幕快照上您具有gate.util.GateException ,但是在异常堆栈中只有gate.util.GateExcep (但可能只是SO问题)。

Let me know if that works. 让我知道是否可行。

Correct me if I'm wrong, but you're not running this jar using ANT are you? 如果我错了,请纠正我,但您不是使用ANT运行此jar,是吗? ANT is only used to build the jar file correct? ANT仅用于正确构建jar文件吗?

There are two ways to launch your java program 有两种启动Java程序的方法

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

The first requires you to set the "Main-Class" and "Class-Path" manifest entries. 第一个要求您设置“ Main-Class”和“ Class-Path”清单条目。 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: 它概述了使用ANT manifestclasspath任务来简化清单中的类路径设置的解决方案:

Cannot find Main Class in File Compiled With Ant 在用Ant编译的文件中找不到主类

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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