Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run java code with Apache Ant

Tags:

java

ant

Here is a part of my build.xml:

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

or

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

Here is an example java code:

public class Test {
    public Test() {
        System.out.print("Test2");
    }
    public static void main(String[] args) {
        System.out.println("Test1");
        new Test();
        while(true) {}
    }
}

If I run this code from command line I have "Test1" and then "Test2". If I run this code using the Ant I have only "Test1".

How can I solve this problem?

like image 837
EvgenyM Avatar asked Jun 30 '26 11:06

EvgenyM


2 Answers

You'll probably find that Ant buffers the output to System.out of your program by line before printing to stdout, and because your program never terminates (the while (true) {}), Ant is waiting for the program to finish before flushing the output of the line. Try changing the Test constructor to use println and you'll see the output.

like image 105
beny23 Avatar answered Jul 03 '26 01:07

beny23


This should solve the problem.

System.out.flush(); 

Add it before you get into an infinite loop. (EDIT:) and after you call new Test()

like image 44
RHT Avatar answered Jul 03 '26 00:07

RHT



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!