对于一个非常简单的应用程序,
package mypackage;
import javax.swing.*;
public class Main {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setSize(400, 400);
frame.setVisible(true);
}
}
以下build.xml文件不起作用。窗口显示的时间似乎很短,然后程序退出。
<?xml version="1.0" encoding="utf-8"?>
<project>
<path id="project.class.path">
<pathelement location="build"/>
</path>
<target name="compile" >
<mkdir dir="build" />
<javac srcdir="src" destdir="build" debug="true" debuglevel="lines,source">
<classpath refid="project.class.path" />
</javac>
</target>
<target name="run" depends="compile">
<java classname="mypackage.Main">
<classpath refid="project.class.path" />
</java>
</target>
<target name="clean" >
<delete failonerror="false" verbose="true">
<fileset dir="build" includes="**/*.class"/>
</delete>
</target>
</project>
但按预期运行以下工作:
ant compile
cd build/
java mypackage.Main
这里是定义不同的目标如下,
<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/YOUR.jar" basedir="build/classes">
<manifest>
<attribute name="Main-Class" value="packageName.classname"/>
</manifest>
</jar>
</target>
<target name="run">
<java jar="build/jar/YOUR.jar" fork="true"/>
</target>
</project>
然后使用
ant compile
ant jar
ant run
命令
我们最终找到了答案。替换此行:
<java classname="mypackage.Main">
这行:
<java classname="mypackage.Main" fork="true">
不过,我希望得到一个更详细地解释为什么会这样的答案。:)