简体   繁体   中英

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);
    }

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.

通过混合使用wmic(以获取Windows进程列表)和taskkill(强制终止正在运行的进程)解决了问题。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM