0

我在 Eclipse 中的第一次 ant 构建遇到了一些麻烦,这是我的 build.xml 构建文件。

<project name="Rutherford" 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"/>
    <property name="dist"  location="dist"/>
    <property name="libs" value="libs"/>

    <path id="classpath">
        <fileset dir="${libs}" includes="**/*.jar"/>
    </path>

    <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 srcdir="${src}" destdir="${build}" classpathref="classpath">
            <compilerarg line="-encoding utf-8"/>
        </javac>
    </target>

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

        <!-- Put everything in ${build} into the MyProject-${DSTAMP}.jar file -->
        <jar jarfile="${dist}/MyProject-${DSTAMP}.jar" basedir="${build}">
            <manifest>
                <attribute name="Main-Class" value="nat.rutherford.DesktopStarter"/>
            </manifest>
        </jar>
    </target>

    <target name="run">
        <java jar="${dist}/MyProject-${DSTAMP}.jar" fork="true"/>
    </target>

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

它编译正常,没有警告或错误,但是当我尝试运行 .jar 时,它显示“找不到主类:nat.rutherford.DesktopStarter。程序现在将退出' =(

我已经阅读了大量关于此事的页面,但到目前为止还没有定论。

我能够使用 Eclipse -> File -> Export -> Java -> Runnable Jar File 编译它。但是我使用了一些 UTF-8 编码的 .txt 文件,它们似乎无法以这种方式处理,我需要它们!即我有希腊字符应该读...dσ/dΩ...但目前读... dÃ/d©...这是行不通的^^

所以基本上我需要让我的 Ant 构建工作,记住它也需要能够处理我的 UTF-8 编码的 .txt 文件。

4

2 回答 2

4

当您创建 jar 时问题出在您的任务分配中。如果你的编译是对的,打包jar的时候没有问题。错误的事情:

  • <mkdir dir="${dist}/lib"/>-> 这没有意思,你永远不要使用它

  • 其次,您没有将库包含在jar中,然后当您尝试执行 jar 时,它不起作用,为什么您会看到错误消息找不到主类:nat.rutherford.DesktopStarter。程序现在将退出您可以使用 Winzip 或类似工具看到您的库不在您的 jar 中。我想当您尝试使用 windows 或类似工具直接执行 jar 时,您会看到您的问题。查看正在发生的事情的好方法,看到控制台中打印的问题正在以下一种方式执行您的 jar:java -jar MyProject-20120102.jar

  • 请参阅:如何将您的库包含在您的 jar 中?

  • 如果您想了解更多关于使用 ant 进行 jar 包装的信息,请试试这个

  • 您需要修改清单中的Class-path属性以将库包含在 ${libs} 文件夹中的另一件事。

于 2012-01-02T14:42:51.373 回答
0

看起来您已向可执行 JAR 添加了一个清单,该清单拼写nat.rutherford.DesktopStarter为您的主类。

我建议您打开 JAR 并验证 manifest.mf 是否出现并且确实说明了您的 Ant build.xml 所做的事情。

我还将验证您是否DesktopStarted.class出现在文件夹路径nat.rutherford中。如果没有,JVM 将找不到它。

于 2012-01-02T14:35:44.830 回答