简体   繁体   English

用Java杀死进程

[英]Kill process in Java

I run process from my Java code like this p = run.exec("cmd /c start \\"\\" C:\\\\<nameof .cmd file>"); 我从我的Java代码运行过程,例如p = run.exec("cmd /c start \\"\\" C:\\\\<nameof .cmd file>"); . At some point, I want to kill this process. 在某些时候,我想终止这个过程。 Calling destroy() method on process kill the process, but I want to turn off command line, where procces is still running. 在进程上调用destroy()方法会杀死该进程,但是我想关闭仍在运行过程的命令行。 When I looked to Task Manager, this process has no name, it has only postfix .exe . 当我查看任务管理器时,此过程没有名称,只有postfix .exe

In Task Manager, it look like this: 在任务管理器中,如下所示: 在此处输入图片说明

So I cannot do this p = run.exec("taskkill /F /IM <nameofexe>.exe"); 所以我不能这样做p = run.exec("taskkill /F /IM <nameofexe>.exe"); , because this running process dont have name. ,因为此运行进程没有名称。

Is there a way, how to completely turn off cmd and kill this running process? 有没有办法完全关闭cmd并杀死该正在运行的进程?

When you launched your process, the CMD call may have launched additional child processes. 启动进程时,CMD调用可能已经启动了其他子进程。 Odds are good your second command line is killing one of the children, but not the CMD itself. 赔率不错,您的第二个命令行正在杀死一个孩子,而不是CMD本身。 The ideal situation would be to kill the launched process, not to run a second command line executable to kill (possibly) one of the children. 理想的情况是杀死启动的进程,而不是运行另一个命令行可执行文件来杀死(可能是)一个子进程。

Process child = run.exec("cmd /c start \"\" C:\\<nameof .cmd file>");
if (timeToKillTheProcess) {
    child.destroy();
    child.waitFor();
}

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

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