【发布时间】:2014-08-25 00:46:29
【问题描述】:
我希望能够终止从我的 java 程序启动的 ping,但我需要获取 pid 才能这样做。我也做了一些研究,发现可以使用SendSignal.exe发送Ctl+C,但是我需要我刚刚启动的进程的pid。 Java中有没有办法获取pid?
Process p = null;
InputStream processOutput;
BufferedReader reader = null;
String line = " ";
p = Runtime.getRuntime().exec("cmd /c " + command);
processOutput = p.getInputStream();
reader = new BufferedReader(new InputStreamReader(processOutput));
// Read the input
while((line = reader.readLine()) != null){
output += line + "\n";
System.out.println(line);
}
【问题讨论】:
-
谢谢。这对我有帮助。不知道为什么我在谷歌搜索时没有看到它。
-
不用担心。我也做过很多次了。
-
不是那个问题的真正重复,因为这个问题是关于子进程的 PID 而不是当前进程的 PID。