在 Java 中使用 ProcessBuilder 将一个进程的输出重定向到另一个进程的输入
Redirecting the output of a process into the input of another process using ProcessBuilder in Java
我有两个由 processBuilders 定义的进程:
ProcessBuilder pb1 = new ProcessBuilder (...)
ProcessBuilder pb2 = new ProcessBuilder (...)
我希望 pb1
的输出成为 pb2
的输入。
我在文档中发现我可以使用 pipe:
从另一个进程读取 pb2
的输入
pb2.redirectInput(Redirect.PIPE);
但是,如何指定我希望此管道从 pb1
的输出中读取?
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 Indicates that subprocess I/O
will be connected to the current Java process over a pipe.
所以我认为其中之一不会帮助您将一个进程的输出重定向到
另一个进程的输入。你必须自己实现它。
实施:
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();
}
}
这个肯定可以设计的更好
我还没有测试过它是否安全运行,但它确实可以。
用法:
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();
}
}
}
输出:
Input process1: echo echo echo
Output process1 / Input process2:echo echo
Output process2: echo
从 JDK 9 开始,您可以像这样使用 startPipeline
:
ProcessBuilder.startPipeline(
Arrays.asList(
new ProcessBuilder(...),
new ProcessBuilder(...),
...
)
)
我有两个由 processBuilders 定义的进程:
ProcessBuilder pb1 = new ProcessBuilder (...)
ProcessBuilder pb2 = new ProcessBuilder (...)
我希望 pb1
的输出成为 pb2
的输入。
我在文档中发现我可以使用 pipe:
pb2
的输入
pb2.redirectInput(Redirect.PIPE);
但是,如何指定我希望此管道从 pb1
的输出中读取?
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 Indicates that subprocess I/O will be connected to the current Java process over a pipe.
所以我认为其中之一不会帮助您将一个进程的输出重定向到 另一个进程的输入。你必须自己实现它。
实施:
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();
}
}
这个肯定可以设计的更好 我还没有测试过它是否安全运行,但它确实可以。
用法:
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();
}
}
}
输出:
Input process1: echo echo echo
Output process1 / Input process2:echo echo
Output process2: echo
从 JDK 9 开始,您可以像这样使用 startPipeline
:
ProcessBuilder.startPipeline(
Arrays.asList(
new ProcessBuilder(...),
new ProcessBuilder(...),
...
)
)