3

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);
    }
Christopher Dancy
  • 656
  • 2
  • 10
  • 21

2 Answers2

2

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.

Community
  • 1
  • 1
Anthony Accioly
  • 21,918
  • 9
  • 70
  • 118
  • Thanks Anthony. I actually tried that previously and it did not work and does not work, as is, with your example. What I did find is that if I appended the correct file extension, using your example, the code does work i.e. "ant.bat -f HelloWorld.xml". – Christopher Dancy Jul 18 '12 at 21:47
  • You are right about including the file extension (I've updated the answer). Also, for programs that aren't in the `PATH` variable you need to call it using either a full or relative path. – Anthony Accioly Jul 18 '12 at 21:54
  • Actually on further testing this does not work :( The spawned ant process is still running in the background ... – Christopher Dancy Jul 19 '12 at 13:14
0

Problem solved by using a mix of wmic (to grab the windows process list) and taskkill (to force kill the running process).

Christopher Dancy
  • 656
  • 2
  • 10
  • 21