java process 重定向,使用Java中的ProcessBuilder将流程的输出重定向到另一个流程的输入中...

I have two processes defined by processBuilders:

ProcessBuilder pb1 = new ProcessBuilder (...)

ProcessBuilder pb2 = new ProcessBuilder (...)

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);

However, how can I specify that I want this pipe to read from the output of 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.

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.

Implementation:

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.

Usage:

RedirectStreams redirectStreams = new RedirectStreams(process1,process2);

Test:

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();

}

}

}

Output:

Input process1: echo echo echo

Output process1 / Input process2:echo echo

Output process2: echo

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
`java.lang.ProcessBuilder` 是 Java 用于创建和管理外进程的类。它提供了一种简单的方式来启动外部命令,并与其进行交互。 要创建一个新的 `ProcessBuilder` 对象,你可以使用以下代码: ```java ProcessBuilder processBuilder = new ProcessBuilder(command); ``` 其,`command` 是一个字符串列表,表示要执行的命令及其参数。例如,如果要执行 `ls -l` 命令,可以这样写: ```java ProcessBuilder processBuilder = new ProcessBuilder("ls", "-l"); ``` 一旦创建了 `ProcessBuilder` 对象,你可以使用其提供的方法来设置执行环境、工作目录、重定向输入输出流等。最后,调用 `start()` 方法启动外部进程并返回一个 `Process` 对象。 以下是一个简单的示例代码,演示了如何使用 `ProcessBuilder` 执行外部命令并获取其输出: ```java import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; public class ProcessBuilderExample { public static void main(String[] args) { try { ProcessBuilder processBuilder = new ProcessBuilder("ls", "-l"); Process process = processBuilder.start(); // 获取命令输出 InputStream inputStream = process.getInputStream(); BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream)); String line; while ((line = reader.readLine()) != null) { System.out.println(line); } // 等待命令执行完成 int exitCode = process.waitFor(); System.out.println("Command exited with code: " + exitCode); } catch (IOException | InterruptedException e) { e.printStackTrace(); } } } ``` 希望这能帮助到你!如果你有任何其他问题,请随时问我。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值