使用Java中的ProcessBuilder将流程的输出重定向到另一个流程的输入中 [英] Redirecting the output of a process into the input of another process using ProcessBuilder in Java
问题描述
我有两个由processBuilders定义的过程:
I have two processes defined by processBuilders:
ProcessBuilder pb1 = new ProcessBuilder (...)
ProcessBuilder pb2 = new ProcessBuilder (...)
我希望pb1
的输出成为pb2
的输入.
我在文档中发现可以使用管道使pb2
的输入从另一个进程中读取:
I want the output of pb1
to be the input to pb2
.
I found in the documentation that I can make the input of pb2
to be read from another process by using pipe:
pb2.redirectInput(Redirect.PIPE);
但是,如何指定我希望此管道从pb1
的输出中读取?
However, how can I specify that I want this pipe to read from the output of pb1
?
推荐答案
static ProcessBuilder.Redirect INHERIT指示子流程I/O 源或目标将与当前的相同 过程.
static ProcessBuilder.Redirect INHERIT Indicates that subprocess I/O source or destination will be the same as those of the current process.
static ProcessBuilder.Redirect PIPE指示子流程I/O 将通过管道连接到当前Java进程.
static ProcessBuilder.Redirect PIPE Indicates that subprocess I/O will be connected to the current Java process over a pipe.
因此,我认为其中之一不会帮助您将一个进程的输出重定向到 另一个过程的输入.您必须自己实施.
So I don't think one of these will help you redirecting the output of one process to the input of another process. You have to implement it yourself.
实施:
public class RedirectStreams {
public RedirectStreams(Process process1, Process process2) {
final Process tmpProcess1 = process1;
final Process tmpProcess2 = process2;
new Thread(new Runnable() {
@Override
public void run() {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(tmpProcess1.getInputStream()));
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(tmpProcess2.getOutputStream()));
String lineToPipe;
try {
while ((lineToPipe = bufferedReader.readLine()) != null){
System.out.println("Output process1 / Input process2:" + lineToPipe);
bufferedWriter.write(lineToPipe + '\n');
bufferedWriter.flush();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
}
}
肯定可以设计得更好 而且我还没有测试它是否可以安全运行,但确实可以完成工作.
This one can surely be designed nicer and I haven't tested, if it runs's safe, but it does the job.
用法:
RedirectStreams redirectStreams = new RedirectStreams(process1,process2);
测试:
public class ProcessPipeTest {
@Test public void testPipe(){
try {
Process process1 = new ProcessBuilder("/bin/bash").start();
Process process2 = new ProcessBuilder("/bin/bash").start();
RedirectStreams redirectStreams = new RedirectStreams(process1,process2);
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(process1.getOutputStream()));
String command = "echo echo echo";
System.out.println("Input process1: " + command);
bufferedWriter.write(command + '\n');
bufferedWriter.close();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process2.getInputStream()));
String actualOutput = bufferedReader.readLine();
System.out.println("Output process2: " + actualOutput);
assertEquals("echo",actualOutput);
} catch (IOException e) {
e.printStackTrace();
}
}
}
输出:
输入过程1:回声回声回声
Input process1: echo echo echo
输出过程1/输入过程2:echo echo
Output process1 / Input process2:echo echo
输出过程2:回声
这篇关于使用Java中的ProcessBuilder将流程的输出重定向到另一个流程的输入中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!