我的构建可以使用下面的build.xml
文件编译并完美运行。
唯一的问题是我创建了一个jar,但是如果没有给出Could not find or load main class
错误,它将无法运行。
我已经阅读了关于常见Could not find or load main class
错误的多个堆栈溢出答案。但是,我在下面的build.xml
中找不到自己做错的事情。
build.xml
<?xml version="1.0" encoding="UTF-8"?>
<project default="run" name="My Project ">
<target name="run" depends="createjar">
<java classname="com.company.program.project.MyMainClass">
<classpath path="staging">
<fileset dir="C:\COMPANY\Eclipse\3.6-64\plugins">
<include name="org.eclipse.swt.*.jar" />
</fileset>
</classpath>
</java>
</target>
<target name="createjar" depends="compile">
<jar destfile="./builds/jars/swtgui.jar" basedir="./staging/com/company/program/project" filesetmanifest="mergewithoutmain">
<manifest>
<attribute name="Main-Class" value="com.company.program.project.MyMainClass" />
</manifest>
<!--
<fileset dir="./bin/com/company/program/project" includes="./*.class" />
-->
<fileset dir="C:\COMPANY\Eclipse\3.6-64\plugins\" includes="org.eclipse.swt.win32.win32.x86_64_3.6.0.v3650b.jar" />
</jar>
</target>
<target name="compile">
<javac includeantruntime="false" srcdir="./src" destdir="staging">
<classpath>
<fileset dir="C:\COMPANY\Eclipse\3.6-64\plugins">
<include name="org.eclipse.swt.*.jar" />
</fileset>
</classpath>
</javac>
</target>
<record name="./MyMainClass.log" loglevel="verbose" action="start"/>
MyProject.log
createjar:
[jar] found a total of 0 manifests in 2 resource collections
[jar] A$1.class added as A$1.class is outdated.
[jar] A$10.class added as A$10.class is outdated.
[jar] A$11.class added as A$11.class is outdated.
[jar] ...
[jar] No sources found.
[jar] Building jar: C:\my_workspace\my_project\builds\jars\swtgui.jar
[jar] adding directory META-INF/
[jar] adding entry META-INF/MANIFEST.MF
[jar] adding entry A$1.class
[jar] adding entry A$10.class
[jar] adding entry A$11.class
[jar] adding entry org.eclipse.swt.win32.win32.x86_64_3.6.0.v3650b.jar
[jar] No Implementation-Title set.No Implementation-Version set.No Implementation-Vendor set.
Location: C:\my_workspace\my_project\build.xml:16:
createjar: duration 0 seconds
正确指定了主类。正确指定了类路径(basedir)。等待编译完成执行。等等
我将我的日志文件留在了下面,但是ant
似乎没有提供任何线索说明为什么找不到主类。有人发现上面两个文件有什么立即出问题吗?
答案 0 :(得分:1)
basedir
任务的jar
属性应与destdir
任务的javac
属性匹配。
javac
任务写为
<javac includeantruntime="false" srcdir="./src" destdir="staging">
因此,jar
任务应写为
<jar destfile="./builds/jars/swtgui.jar" basedir="staging" filesetmanifest="mergewithoutmain">
由于您的软件包定义为com.company.program.project
,因此Java编译器和JVM会将您的类放在文件夹com/company/program/project
中,但是您当前的jar
任务会将它们放到根文件夹中jar文件。
答案 1 :(得分:0)
您是否尝试过Eclipse->导出->可运行的jar文件选项?
当我尝试从我的测试摆动应用程序手动创建一个jar时,我也遇到了这个问题。但是当我尝试此选项时,它起作用了。