Java Kill进程以ProcessBuilder开始

时间:2016-09-05 10:37:34

标签: java unix process processbuilder

我正在运行这样的流程:

builder = new ProcessBuilder("/bin/bash", "-c", "./MessageGenerator | ./SimpleEchoServer");
process = builder.start();

然后process.destroy()或forciblyDestroy()不起作用。是因为我正在使用烟斗吗?我怎样才能杀死这些进程?

1 个答案:

答案 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();
    }
}