RockManX77777
RockManX77777

Reputation: 171

Killing a java.lang.Process not killing subprocess()

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?

Upvotes: 3

Views: 3229

Answers (2)

Ahmed Nabil
Ahmed Nabil

Reputation: 19056

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

Upvotes: 4

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 https://bugs.java.com/bugdatabase/view_bug?bug_id=4770092).

Upvotes: 0

Related Questions