编译和JAR文件创建都是成功的。
通过 ant 文件运行 java 文件会产生错误。
<project>
<target name="clean">
<delete dir="build"/>
</target>
<target name="compile">
<mkdir dir="build/classes"/>
<javac srcdir="src" destdir="build/classes"/>
</target>
<target name="jar">
<mkdir dir="build/jar"/>
<jar destfile="build/jar/Helloworld" basedir="build/classes">
<manifest>
<attribute name="Main-Class" value="Helloworld"/>
</manifest>
</jar>
</target>
<target name="run">
<java jar="build/jar/Helloworld" fork="true"/>
</target>
</project>
构建文件:C:Workspaceanttestbuild.xml
run:
[java] java.lang.NoClassDefFoundError: Helloworld
[java] Caused by: java.lang.ClassNotFoundException: Helloworld
[java] at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
[java] at java.security.AccessController.doPrivileged(Native Method)
[java] at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
[java] at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
[java] at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
[java] at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
[java] Could not find the main class: Helloworld. Program will exit.
[java] Exception in thread "main"
[java] Java Result: 1
构建成功
Ant 默认忽略 Java 进程的返回代码,请参阅: https://ant.apache.org/manual/Tasks/java.html#failonerror 您可以添加 failonerror="true" 属性来更改此行为,即:
尝试更改
<target name="run">
<java jar="build/jar/Helloworld" fork="true"/>
</target>
自
<target name="run">
<java jar="build/jar/Helloworld" failonerror="true" fork="true"/>
</target>
您的 JAR 文件将不包含HelloWorld
类。