【问题标题】:Failing to build working .jar file using ANT无法使用 ANT 构建工作 .jar 文件
【发布时间】:2017-07-25 13:41:56
【问题描述】:

我在 Eclipse 中制作了一个简单的“AntExecutor”应用程序,它可以以编程方式运行 ant 任务并且它可以工作。但出于大学目的,我需要让它独立于 IDE。所以,有趣的是,我正在努力创建可以编译的 ant 任务,构建我的“AntExecutor”应用程序(执行 ant 任务):)

我目前正在尝试定义 ant-tasks 的精简版本仅包含一个源文件在 'storageAccess' 包中:

./src/storageAccess/AntExecutor.java

我有一些 AntExecutor.java 使用的库:

./lib

构建文件位于:

./build.xml

AntExecutor.java 还需要 ant 库来执行 ant 任务,因此它们会在编译时添加到 CP。在构建文件中:

<classpath path="${build};D:/DevTools/apache-ant-1.9.8/lib/;"/>

完整的 build.xml 文件:

<project name="AntExecutor" default="dist" basedir=".">
    <description>
        simple example build file
    </description>
  <!-- set global properties for this build -->
  <property name="src" location="src"/>
  <property name="build" location="build/classes/"/>
  <property name="dist"  location="build/jar/"/>

  <target name="init">
    <!-- Create the time stamp -->
    <tstamp/>
    <!-- Create the build directory structure used by compile -->
    <mkdir dir="${build}"/>
  </target>

  <target name="compile" depends="init"
        description="compile the source " >
    <!-- Compile the java code from ${src} into ${build} -->
    <javac  destdir="${build}">
      <src path="${src}"/>
      <classpath path="${build};D:/DevTools/apache-ant-1.9.8/lib/;"/>

    </javac>
  </target>

  <target name="dist" depends="compile"
        description="generate the distribution" >
    <!-- Create the distribution directory -->
    <mkdir dir="${dist}"/>

    <!-- Put everything in ${build} into RunExecutor.jar file -->
  <jar destfile = "${dist}/RunExecutor.jar" basedir="${build}">

   <manifest>
      <attribute name = "Main-Class" value = "storageAccess.AntExecutor"/>
      <attribute name = "Class-Path" value = "D:/DevTools/apache-ant-1.9.8/lib/;"/>
   </manifest>
  </jar>

  <copy todir="${dist}\lib">
   <fileset dir="lib"/>
  </copy>


  </target>

  <target name="clean"
        description="clean up" >
    <!-- Delete the ${build} and ${dist} directory trees -->
    <delete dir="${build}"/>
    <delete dir="${dist}"/>
  </target>
</project>

现在,如果我运行 'ant dist' 命令,我没有收到任何错误,构建成功,并且在 ./build/jar 创建 RunExecutor.jar 文件

为了检查 RunExecutor.jar 的内容,我运行了:jar tf build/jar/RunExecutor.jar

结果:

META-INF/
META-INF/MANIFEST.MF
storageAccess/
storageAccess/AntExecutor.class

看来 storageAcces.AntExecutor 类确实已成功编译为 .jar 文件。

但是,如果我尝试像这样运行它:java -jar build/jar/RunExecutor.jar

我收到此错误:

Error: Could not find or load main class storageAccess.AntExecutor

主要问题:

为什么找不到明确在其中的类。(如“jar tf”所示)我该如何解决?

另外,将 ant/lib/*.jar 文件添加到 CP 以编译和运行“RunExecutor.jar”的正确方法是什么? 可以像我现在一样指定它们的路径吗? :

<attribute name = "Class-Path" value = "D:/DevTools/apache-ant-1.9.8/lib/;"/>

或者,也许我应该使用通配符,例如:

<attribute name = "Class-Path" value = "D:/DevTools/apache-ant-1.9.8/lib/*.jar;"/>

或者,我应该沮丧地一个一个地添加所有文件吗?

<attribute name = "Class-Path" value = "D:/DevTools/apache-ant-1.9.8/lib/ant.jar;"/> , etc...

【问题讨论】:

  • 在阅读了 Mark 的建议后,我使用该线程中建议的 属性为我的 jar 文件重新定义了 ant 库的类路径。这解决了问题。但是,并没有明确说明为什么它解决了这个问题。我没有收到`java.lang.NoClassDefFoundError`(正如 Marks 中的人建议的线程)。我的 Main-Class 没有找到,而且 Main 方法之前甚至没有运行过。我无法理解以不同方式链接 ant 库如何解决问题。

标签: java ant jar


【解决方案1】:

问题

<attribute name = "Class-Path" value = "D:/DevTools/apache-ant-1.9.8/lib/;"/>

你是硬编码路径,这不是一个好习惯。这个 jar 不会在其他机器上执行,因为它们有可能在同一位置没有 lib。

您可以直接从 eclipse 项目本身创建可执行 jar。步骤请参考this

您也可以将所需的库放在同一个 jar 文件中,默认情况下它们会添加到类路径中。

【讨论】:

    猜你喜欢
    • 2013-04-12
    • 1970-01-01
    • 2012-04-06
    • 2012-04-02
    • 1970-01-01
    • 2020-04-08
    • 2013-06-23
    • 2013-09-23
    • 2013-07-15
    相关资源
    最近更新 更多