这是我的 build.xml 的一部分:

<target name="run">
    <java jar="${jar.dir}/${Main.class}.jar"
        fork="yes"
        <assertions>
            <enable />
        </assertions>
    </java>
</target>

要么
<target name="run">
    <java classname="${Main.class}" classpath="${classes.dir};${lib.dir}" fork="yes"/>
</target>

这是一个示例 Java代码:
public class Test {
    public Test() {
        System.out.print("Test2");
    }
    public static void main(String[] args) {
        System.out.println("Test1");
        new Test();
        while(true) {}
    }
}

如果我从命令行运行此代码,则将得到“Test1”,然后是“Test2”。
如果我使用Ant运行此代码,则只有“Test1”。

我怎么解决这个问题?

最佳答案

您可能会发现,Ant在打印到stdout之前逐行将输出缓冲到程序的System.out中,并且由于您的程序永不终止(while (true) {}),因此Ant会在刷新行的输出之前等待程序完成。尝试将Test构造函数更改为使用println,您将看到输出。

07-28 03:24
查看更多