我正在运行这样的流程:
builder = new ProcessBuilder("/bin/bash", "-c", "./MessageGenerator | ./SimpleEchoServer");
process = builder.start();
然后process.destroy()或forciblyDestroy()不起作用。是因为我正在使用烟斗吗?我怎样才能杀死这些进程?
答案 0 :(得分:0)
你还应该拆分管道命令。我写了一个小例子:
package com.company;
import java.io.*;
public class Main {
public static void main(String[] args) throws IOException, InterruptedException {
ProcessBuilder processBuilder;
String line;
processBuilder = new ProcessBuilder("/bin/bash", "-c", "ls -l", "|", "grep java");
Process process = processBuilder.start();
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
while ((line = reader.readLine()) != null) {
System.out.println ("Stdout: " + line);
}
Thread.sleep(10000);
process.destroy();
}
}