Close a running program from java application
I open up an external application from my java application. How can I close thi开发者_StackOverflow社区s application from the same Java application? thanks
The best you can do (without venturing into messy/complicated/platform specific stuff) is to call Process.kill()
on the Process object you got when you started the external application.
I don't think this is guaranteed to close the application*, and there is a chance that it may cause it to close uncleanly; i.e. without giving the application a chance to save open files, etc.
* Indeed, on *NIX if you started a "setuid root" process from a non-root Java application, and the OS won't let it send any signals to to.
Why don't you have a batch (Windows) or script (*nix) file that start and stops that application, and then run your runtime.exec with the parameter start/stop?
UPDATE:
This may help: http://it.toolbox.com/blogs/database-solutions/kill-a-process-in-windows-bat-19875
Second Update
You can also search by exe name using: 'tasklist ^| findstr /i excel.exe'
On Windows this will fail. It's a top 25 bug (or maybe top 25 rfe), though it really isn't so much Java's fault ... On Windows any children of the parent process will not be killed when parent is killed..... and there are many ways to run afoul of this (cmd /c anything and you will be in game-over-land)
精彩评论