简体   繁体   English

从java应用程序关闭正在运行的程序

[英]Close a running program from java application

I open up an external application from my java application. 我从我的java应用程序中打开了一个外部应用程序。 How can I close this application from the same Java application? 如何从同一Java应用程序关闭此应用程序? 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. 你可以做的最好的事情(没有冒险进入凌乱/复杂/平台特定的东西)是在启动外部应用程序时调用Process对象上的Process.kill()

I don't think this is guaranteed to close the application * , and there is a chance that it may cause it to close uncleanly; 我不认为这可以保证关闭申请* ,并且有可能导致它不洁地关闭; ie 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. *实际上,如果您从非根Java应用程序启动“setuid root”进程,则在* NIX上,操作系统不会让它发送任何信号。

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? 为什么没有启动和停止该应用程序的批处理(Windows)或脚本(* nix)文件,然后使用参数start / stop运行runtime.exec?

UPDATE: 更新:

This may help: http://it.toolbox.com/blogs/database-solutions/kill-a-process-in-windows-bat-19875 这可能有所帮助: 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' 您还可以使用以下命令搜索exe名称: 'tasklist ^| findstr /i excel.exe' 'tasklist ^| findstr /i excel.exe'

On Windows this will fail. 在Windows上,这将失败。 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) 这是一个前25个错误(或者可能是前25个错误),虽然它确实不是Java的错误......在Windows上,当父母被杀时,父进程中的任何子进程都不会被杀死.....并且很多方法可以解决这个问题(cmd / c任何东西,你将会在陆地上游戏)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM