如何杀死由java中的进程启动的子进程? [英] How to kill a child process started by process in java?
本文介绍了如何杀死由java中的进程启动的子进程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在下面的代码片段中,如果我使用 p.destroy()
销毁 Process p
只处理 p
(iecmd.exe
) 正在被破坏.但不是它的子iperf.exe
.如何在 Java 中终止此进程.
In the below code snippet, if I destroy Process p
using p.destroy()
only process p
(i.e.cmd.exe
) is getting destroyed. But not its child iperf.exe
. How to terminate this process in Java.
Process p= Runtime.getRuntime().exec("cmd /c iperf -s > testresult.txt");
推荐答案
在 Java 7 ProcessBuilder 可以为你做重定向,所以直接运行iperf而不是通过cmd.exe
.
In Java 7 ProcessBuilder can do the redirection for you, so just run iperf directly rather than through cmd.exe
.
ProcessBuilder pb = new ProcessBuilder("iperf", "-s");
pb.redirectOutput(new File("testresult.txt"));
Process p = pb.start();
结果 p
现在是 itext 本身,所以 destroy()
将按您的要求工作.
The resulting p
is now itext itself, so destroy()
will work as you require.
这篇关于如何杀死由java中的进程启动的子进程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文