Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to kill Ant program spawned from within java

Destroying spawned ant process, from windows, does not work. Unix variants this works fine but from windows this does not work. Code snippet is below. While the return code is correct (1) the spawned process continues to execute until done. Only a problem on a windows. Any ideas?

        ProcessBuilder build = new ProcessBuilder();
    List<String> list = build.command();
    list.add("cmd");
    list.add("/C");
    list.add("ant");
    list.add("-f");
    list.add("HelloWorld.xml");

    try {
        Process p = build.start();          
        Thread.sleep(5000);
        p.destroy();        
        int i = p.waitFor();
        System.out.println(i);
    } catch (Exception e) {
        System.out.println(e);
    }
like image 556
Christopher Dancy Avatar asked Dec 02 '25 02:12

Christopher Dancy


1 Answers

The problem is that Process.destroy doesn't kill the process grandchildren. There is a bug opened for it since 2002.

Anyway, why are you spawning a new prompt with cmd /c start to call Ant? If that is not a requirement just call ant.bat -f HelloWorld.xml.

UPDATE

ant.bat will also spawn children processes. There is a workaround with taskkill that may help.

like image 78
Anthony Accioly Avatar answered Dec 03 '25 15:12

Anthony Accioly



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!