3

I start a JAR file from my java swing file using Process proc Runtime.getruntime.exec(); and I need to shut it down so it works on all operating systems.

I've tried proc.detroy(); but that doesn't let it fully shut down and will cause problems with the program.

What do I need to do to get it to shut down safely on all operating systems?

I assume I will need to check what OS is being used and handle each one separately but what should be done to get it to safely shut down on each one?

Here is the StartStop.java code:

public class StartStop {

    static Process proc = null;

    public void program() {
        new Thread() {
            public void run() {
                try {
                    proc = Runtime.getRuntime().exec("java -jar DEDServer_release.jar");

                    String res = org.apache.commons.io.IOUtils.toString(new BufferedReader(new InputStreamReader(proc.getErrorStream())));
                    if(res.contains("Error")){
                        JOptionPane.showMessageDialog(null, "Failed to start DEDServer. Make sure that this file is in the same directory as DEDServer_release.jar");
                    }

                }catch (IOException e) {
                    JOptionPane.showMessageDialog(null, "Failed to start DEDServer. Make sure that this file is in the same directory as DEDServer_release.jar");
                }
            }
        }.start();

    }

}

Thank you for your help.

Shivam Paw
  • 203
  • 3
  • 14
  • JOptionPane must be wrapped inside invokeLater, more read in Oracle tutorial Initial Thread – mKorbel Jun 06 '15 at 13:36
  • Why does process.destroy() not work? – JP Moresmau Jun 06 '15 at 13:46
  • @JPMoresmau It's stated in the question... "I've tried proc.detroy(); but that doesn't let it fully shut down and will cause problems with the program." – Shivam Paw Jun 06 '15 at 13:48
  • Well process.destroy() kills the subprocess, that's what it's supposed to do. The whole point of Java is that you do NOT have to write platform specific code. So you should understand why your program doesn't shut down when requested. Probably a better approach is to be able to send your DEDServer program a command to make it close down cleanly. – JP Moresmau Jun 06 '15 at 13:53
  • And you say "will cause problems with the program". What problems? – JP Moresmau Jun 06 '15 at 13:53
  • So how can I send it a command then? I assume I need to do ctrl+c for windows, cmd+c for osx etc.. @JPMoresmau – Shivam Paw Jun 06 '15 at 13:54
  • @JPMoresmau problems when the DEDServer program is saving to the database. – Shivam Paw Jun 06 '15 at 13:54
  • 1
    You need to read http://stackoverflow.com/questions/2950338/how-can-i-kill-a-linux-process-in-java-with-sigkill-process-destroy-does-sigte, http://stackoverflow.com/questions/191215/how-to-stop-java-process-gracefully. What I'm suggesting is not to go through the os specific way, but have your server code listen to maybe standard input for a "exit" command. When it receives that command, it closes down gracefully. – JP Moresmau Jun 06 '15 at 13:57

1 Answers1

3

Java8 introduces a more powerful API called destroyForcibly(), try using it,

below is the documentation for the same,

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.

Also, i would suggest you to take a look at this post for more information if destroyForcibly doesn't work.

Community
  • 1
  • 1
Sathish
  • 4,975
  • 3
  • 18
  • 23
  • But that gives the same problem as detroy(). It will mean that the other program won't get to shutdown safely. – Shivam Paw Jun 06 '15 at 14:01
  • Yep, then try the techniques listed in this post - http://stackoverflow.com/questions/6356340/killing-a-process-using-java – Sathish Jun 06 '15 at 14:04