Java-Ant构建(Eclipse)-找不到主类:nat.rutherford.DesktopStarter
我在eclipse中第一次使用ant构建时遇到了一些问题,下面是我的build.xml构建文件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 -->
<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中,那么当您尝试执行jar时,它不起作用,这就是为什么您看到错误消息找不到主类:nat.rutherford.DesktopStarter。程序现在将退出,您可以使用Winzip o看到您的库不在jar中r类似。我想当您尝试直接使用windows或类似工具执行jar时,您看到了您的问题。查看发生了什么的一个好方法是,查看控制台中打印的问题是以下一种方式执行jar:
java-jar MyProject-20120102.jar
- 见:
- 如果您想了解更多关于使用ant进行jar打包的信息
- 另外,您需要修改清单中的类路径属性,以将库包含在${libs}文件夹中