I am creating a java swing application when allows the use to execute multiple commands for the command line. I have already added this line of code to ensure my swing application exits on close:
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
However, how can I make sure all processes invoked by the swing application are all destroyed if I close the gui. This is my code to execute a command through the gui:
Process p;
String command = "java -jar blah blah blah";
try {
System.out.println("Export Command To Execute: " + command);
p = Runtime.getRuntime().exec(command);
p.waitFor();
//BufferedReader inputStream = new BufferedReader(new InputStreamReader(p.getInputStream()));
BufferedReader errorStream = new BufferedReader(new InputStreamReader(p.getErrorStream()));
String line = "";
String output ="";
while((line = errorStream.readLine()) != null) {
output+=line;
}
//inputStream.close();
errorStream.close();
System.out.println(output);
System.out.println("Export complete!...");
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
The user of the application can execute multiple commands in the application, and each command can take a while to run, so I want to make sure all the processes created by the application are killed instead of letting them run continuously if the user choose to close the application.