Вопрос

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

Это было полезно?

Решение

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()

Другие советы

You can do:

Process p2  = runtime.exec("c:\\windows\\system32\\taskkill /IM program.exe")

it is a standard kill not a forceful one (that requires /F), hence it will simply notify the program it should terminate spontaneously.

I could not try with your program, but I opened notepad typed some text and before saving, I used this way to terminate it and notepad asked me if I wanted to save my work.

With the /F option notepad was terminated immediately leaving the file unsaved.

This is the same difference you have in unix between kill vs and kill -9.

Note that this will terminate all the instances of you process, otherwise you should use the pid.

And if your program upon signaling will be able to comply and try to close the open connections and terminate, it will depend on the program itself.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top