2

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

2 Answers 2

4

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()
Sign up to request clarification or add additional context in comments.

7 Comments

The only issue with this method is that it kills the process right away. Which if in the middle of an update may cause the program to corrupt the database. Is there no way to initiate a program to start to shut down?
Normal databases would be protected against such failure :) But I have no idea about Access.
Yeah normal databases are, however the software we use corrupts if it shuts down incorrectly. Which is a royal pain
What you also can do is not to destroy from Java. If you own the other program source code, you could let the java program create some kind of signal file. And the other program periodically check for the existence of such a file. If it is found, it can shutdown itself when it is ready for it.
If there is no way to deactive that program in a safe way, this problem is a very difficult one to solve.
|
1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.