Search code examples
javaswingprocessterminate

What is the ideal way to terminate a java subprocess? process.destroyForcibly() doesn't terminate my process


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?

Solution

  • 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