Java - Ant 构建(Eclipse) - 找不到主类 : nat. rutherford.DesktopStarter

标签 java eclipse ant build.xml

我在 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 -> 文件 -> 导出 ->Java -> 可运行 Jar 文件来编译它。但我使用一些 UTF-8 编码的 .txt 文件,它似乎无法以这种方式处理,而我需要它们!即我有希腊字符应该读...dσ/dΩ...但目前读...dà/d©...这是行不通的 ^^

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

最佳答案

问题出在您创建 jar 时的任务 dist 中。如果你的编译正确并且打包jar的时候没有问题。错误之处:

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

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

  • 参见:How to include your libraries in you jar?

  • 如果你想了解更多关于使用 ant 进行 jar 打包的信息 try this .

  • 您还需要修改 manifest 中的 Class-path 属性,以将库包含在 ${libs} 文件夹中。

关于Java - Ant 构建(Eclipse) - 找不到主类 : nat. rutherford.DesktopStarter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8701982/

相关文章:

tomcat - 我怎样才能让 Tomcat 接受我的 OpenJPA 增强类?

ant - Ant:将同一文件集复制到多个位置

regex - 将目录内容打印到文件的 ant 任务

java - Android Studio Proguard 运行失败

java - 使用 ant 编辑/追加数据到文本文件

java - 如何获取构建项目时使用的 eclipse 版本

Android,Logcat 窗口消失了

java - Eclipse 不导入项目

java - 在每 100 行 10 000 上使用 flush() 方法会减慢事务

java - setDisplayHomeAsUpEnabled()错误: non-static method cannot be referenced from a static context