2

I have a GUI that has many buttons. Each button creates a Process (ProcessBuilder.start()) that send of a predetermined ClearCase command with pre-set arguments.

After calling ProcessBuilder.start(), I add the process to a list. When the program shuts down, I iterate through this list and call destroy() on each process. I'm using Runtime.getRuntime().addShutdownHook() to try to iterate through my list.

However, when I look in Windows Task Manager, I see that some conhost.exe and cleartool.exe processes are still alive. I suspect Process.destroy() is either not working or not propagating. Is there a way to completely clean up the program and kill all subprocesses when the user clicks X?

RockManX77777
  • 151
  • 1
  • 7

2 Answers2

2

With Java 9, you can kill the subprocess of the main process reference.

LOGGER.info("Going to stop the process descendants (subprocesses) {}", this.process.descendants().collect(Collectors.toList()));
process.descendants().forEach((ProcessHandle d) -> {
    d.destroy();
});

LOGGER.info("Going to stop the process {}", this.process);
process.destroy();
Ahmed Nabil
  • 17,392
  • 11
  • 61
  • 88
0

see: Killing a process using Java

But be aware that if the process that you invoke creates new sub-processes, those may not be terminated (see http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4770092).

Community
  • 1
  • 1