Java - Ant build (Eclipse) -找不到主类:nat.rutherford.DesktopStar



我在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Ω…但目前读…dA/d©……这是行不通的^^

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

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

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

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

  • 参见:如何在jar中包含库?

  • 如果你想了解更多关于使用ant进行jar打包的信息,可以试试这个

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

看起来你已经添加了一个清单到你的可执行JAR,拼出nat.rutherford.DesktopStarter作为你的主类。

我建议您打开JAR并验证清单。. mf出现了,并且确实说明了Ant build.xml的功能。

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

最新更新