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

Java-Ant构建(Eclipse)-找不到主类:nat.rutherford.DesktopStarter,java,eclipse,ant,build.xml,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 -->

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


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

看起来您已经在可执行JAR中添加了一个清单,将
nat.rutherford.DesktopStarter
作为您的主类

我建议您打开JAR并验证manifest.mf是否出现,以及是否确实说明了Ant build.xml的功能


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

看起来您已经在可执行JAR中添加了一个清单,将
nat.rutherford.DesktopStarter
作为您的主类

我建议您打开JAR并验证manifest.mf是否出现,以及是否确实说明了Ant build.xml的功能


我还将验证您的
桌面启动.class
是否显示在文件夹路径
nat.rutherford
中。如果没有,JVM将找不到它。

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

  • ->这并不意味着你永远不会使用它

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

  • 见:

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

  • 另外,您需要修改清单中的类路径属性,以将库包含在${libs}文件夹中


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

  • ->这并不意味着你永远不会使用它

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

  • 见:

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

  • 另外,您需要修改清单中的类路径属性,以将库包含在${libs}文件夹中


我使用测试类检查了清单,从JAR读取清单,它打印主要属性,输出为;-Ant版本:Apache Ant 1.7.1-清单版本:1.0-创建人:20.4-b02(Sun Microsystems Inc.)-Main Class:nat.rutherford.DesktopStart这个JAR在路径nat/rutherford中不包含一个名为DesktopStart.Class的文件。是的,看起来确实是这样,但我不知道它不在那里我做错了什么。据我所知它应该在那里?!这个.Class文件存在于文件夹“build”中,这就是我创建JAR的地方清单是正确的,那么我遗漏了什么呢?这是我第一次构建,所以它可能是我没有看到的一些基本内容。Jenaiz告诉了你答案-你的Ant build.xml是不正确的。然后我显然遗漏了一些内容,因为我没有看到它。lol。谢谢你的帮助。我使用测试类查看了清单在JAR上,它打印主要属性,输出为;-Ant版本:ApacheAnt 1.7.1-Manifest版本:1.0-创建人:20.4-b02(Sun Microsystems Inc.)-Main Class:nat.rutherford.DesktopStart这个JAR在路径nat/rutherford中不包含一个名为DesktopStart.Class的文件。是的,看起来确实是这样,但我不知道它不在那里我做错了什么。据我所知它应该在那里?!这个.Class文件存在于文件夹“build”中,这就是我创建JAR的地方清单是正确的,那我遗漏了什么?这是我第一次