1

I have a Java Swing application which creates a subprocess. The main Swing application has a stop button which when hit should terminate the subprocess immediately. "process.destroy()" didn't work.

Process myProcess = new ProcessBuilder("java", "-classpath", System.getProperty("java.class.path"), "MyClass.java");
try {
    myProcess.waitFor();
} 
catch (Exception e) {
    e1.printStackTrace();
}

...

myStopButton.addActionListener(new ActionListener() {

    @Override
    public void actionPerformed(ActionEvent e) {
        myProcess.destroyForcibly(); 
        // myProcess is a really long and complex process. So I could not destroy that using process.destroyForcibly().
    }
}
  1. Can I send some signal to the subprocess to call System.exit() internally?
  2. Can I use "taskkill" to kill the sub process ONLY?
smb
  • 13
  • 1
  • 6
  • 1
    Could you please show us how you start the subprocess and how you try to suspend it… – deHaar Aug 30 '18 at 06:21
  • If you have *not* working code, please provide a [mcve], otherwise we can't help you. Beyond that, stopping processes is a solved, well documented topic. So: please do solid research prior posting questions here. – GhostCat Aug 30 '18 at 06:33
  • And then: System.exit() kills your JVM, including all subprocesses. But also the parent one, so that is not what you intend to use (most of the time). – GhostCat Aug 30 '18 at 06:43
  • If I call System.exit() inside the new process, why would that stop the main process? – smb Aug 30 '18 at 06:48
  • I had to post this question since most of the solutions didn't work for me. For instance, see the java documentation for destory(): "Kills the process. Whether the process represented by this Process object is normally terminated or not is implementation dependent." – smb Aug 30 '18 at 06:53

1 Answers1

6

It really depends on your java version.

Prior to Java 8

public abstract void destroy()

Kills the subprocess. The subprocess represented by this Process object is forcibly terminated.

Java 8

public abstract void destroy()

Kills the subprocess. Whether the subprocess represented by this Process object is forcibly terminated or not is implementation dependent.

public Process destroyForcibly()

Kills the subprocess. The subprocess represented by this Process object is forcibly terminated. The default implementation of this method invokes destroy() and so may not forcibly terminate the process. Concrete implementations of this class are strongly encouraged to override this method with a compliant implementation. Invoking this method on Process objects returned by ProcessBuilder.start() and Runtime.exec(java.lang.String) will forcibly terminate the process.

Note: The subprocess may not terminate immediately. i.e. isAlive() may return true for a brief period after destroyForcibly() is called. This method may be chained to waitFor() if needed.

Returns: the Process object representing the subprocess to be forcibly destroyed.

Java 9

public abstract void destroy​()

Kills the process. Whether the process represented by this Process object is normally terminated or not is implementation dependent. Forcible process destruction is defined as the immediate termination of a process, whereas normal termination allows the process to shut down cleanly. If the process is not alive, no action is taken. The CompletableFuture from onExit() is completed when the process has terminated.

public Process destroyForcibly​()

Kills the process forcibly. The process represented by this Process object is forcibly terminated. Forcible process destruction is defined as the immediate termination of a process, whereas normal termination allows the process to shut down cleanly. If the process is not alive, no action is taken. The CompletableFuture from onExit() is completed when the process has terminated.

Invoking this method on Process objects returned by ProcessBuilder.start() and Runtime.exec(java.lang.String) forcibly terminate the process.

API Note:

The process may not terminate immediately. i.e. isAlive() may return true for a brief period after destroyForcibly() is called. This method may be chained to waitFor() if needed. Implementation Requirements: The default implementation of this method invokes destroy() and so may not forcibly terminate the process. Implementation Note: Concrete implementations of this class are strongly encouraged to override this method with a compliant implementation.

Returns:

the Process object representing the process forcibly destroyed

Sagar Gandhi
  • 925
  • 6
  • 20