Search code examples
javaapplication-shutdown

Java terminating a program


Firstly i have googled this question and the only answers i seem to get are using System.exit(0); which isn't what i am looking for. Currently i am opening a program using

Runtime runtime = Runtime.getRuntime();
runtime.exec("C:\\Program Files\\program folder\\program.exe");

This sucessfully opens the program. However closing the program looks like a completely different kettle of fish. Is it possible to close the program through java due to permissions and security? Also can this be done using runtime or is this more complex issue? As i can see that a windows only solution would be to use

taskkill /IM program.exe

However my question would be, if i initiated a taskkill would this shut down the program straight away? or would it tell the program to start to initiate the shutdown process?

Edit:

I Will advise that the program that will be initiated will deal with an access database


Solution

  • You get a Process back when you do the runtime.exec. On that process instance, there is a destroy method:

    http://docs.oracle.com/javase/7/docs/api/java/lang/Process.html

    Runtime runtime = Runtime.getRuntime();
    Process p  = runtime.exec("C:\\Program Files\\program folder\\program.exe");
    p.destroy()