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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With