繁体   English   中英

Java ANT:build.xml问题

[英]Java ANT : Issues with build.xml

我想知道是否有人可以帮助我解决一个严重的问题……我需要使用很多依赖项从一个项目中创建一个jar……问题是:我自己编写的build.xml文件无法正常工作。 更具体地说,它运行没有问题,只是一个警告:

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

但是不幸的是我仍然得到一些classNotFoundDefError


我没有打印出完整的跟踪记录,因为这是我尝试实例化的第一类,但没有找到...所以我猜没有任何效果...但是在这里:

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

有我的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>

如您所见,GateException确实位于gate.jar中。

在此处输入图片说明

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

现在,如果要创建一个包含所有jar库中所有类的 jar文件则必须以稍有不同的方式构建jar文件。 您无法构建包含其他jar的可执行jar(带有Main-Class属性的jar)。 标准类加载器不会在一个jar中查找其他jar文件。 因此,您需要将库jar类扩展最终的jar中。

Ant使用如下所示的方式对此提供了支持。 zipgroupfileset是一项任务,它将获取jar文件列表,将其展开并将类本身包含到目标jar中。 我过滤掉manifest.mf和属性文件,以免它们与我自己的文件发生冲突(例如库jar中的log4j.properties文件)。

这将创建一个包含所有lib jar的所有内容的jar。 清单文件将发生冲突,最后看到的清单文件将获胜。

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

与单个jar一起使用的zipfileset可以从所需的一个jar中过滤掉所有内容。 这将删除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>

确保您的lib文件夹在您的类路径中。 我也有类似的经历,就像你一样。 构建是可以的,因为蚂蚁找到了罐子。 如果要构建jar,请确保清单将jar添加到清单文件中的类路径中。 有一个清单选项,您可以让ant将清单文件添加到jar中。 如果没有其他要求,请在命令行上运行程序,然后使用-classpath选项指向您的lib文件夹。 我认为您的问题就在您的课堂上,尽管我已经战斗了很多次。

这不是答案,但是评论太久了,所以...

您可以执行以下步骤:

  1. gate.util.GateExcept添加简单的main方法
public static void main( final String[] args ) {
    System.out.println( GateExcept.class.getName() );
}
  1. 再次构建jar(将其命名为myjar.jar)

  2. 运行此命令

    java -cp myjar.jar gate.util.GateExcept

  3. 如果工作正常,请尝试(可执行jar)

    java -jar myjar.jar

在我看来,在屏幕快照上您具有gate.util.GateException ,但是在异常堆栈中只有gate.util.GateExcep (但可能只是SO问题)。

让我知道是否可行。

如果我错了,请纠正我,但您不是使用ANT运行此jar,是吗? ANT仅用于正确构建jar文件吗?

有两种启动Java程序的方法

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

第一个要求您设置“ Main-Class”和“ Class-Path”清单条目。 第二个要求您显式列出每个依赖项。

我怀疑您正在尝试使用前者,但是忘记了包括类路径.....以下问题也有同样的问题。 它概述了使用ANT manifestclasspath任务来简化清单中的类路径设置的解决方案:

在用Ant编译的文件中找不到主类

暂无
暂无

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

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