使用Java 7中的ProcessBuilder构建流程管道 [英] Building a process pipe with ProcessBuilder in Java 7
问题描述
我一直试图弄清楚如何使用新的 ProcessBuilder
在Java中管理一些进程。我找不到一个合适的例子来说明我想要做什么,当我尝试自己做这个过程时,这个过程就会挂起。我将非常感谢一些运行相当于 cat test.txt |的代码的简单示例wc
,但不是通过shell。
I've been trying to figure out how to pipe a few processes in Java using the new ProcessBuilder
. I can't find a suitable example of what I want to do and when I try to do it myself the process just hangs. I would appreciate a very simple example of some code that runs the equivalent of cat test.txt | wc
, but not through a shell.
- 更新 -
好的,只是澄清一下。我知道有一些方法可以通过读写流来模拟管道。我想知道是否通过 redirectInput 和 redirectOutput Java 7中引入的方法。
OK, just to clarify. I know there are ways to simulate a pipe by reading and writing streams. I'm wondering if that's done in some automatic way by the redirectInput and redirectOutput methods introduced in Java 7.
推荐答案
在这种情况下你不需要管道。
You don't need a pipe in this case.
"grep bla test.txt"
但是假设你需要一个管道,你需要使用像bash这样的shell
however assuming you need a pipe, you need to use a shell like bash
"/bin/bash", "-c", "grep foo text.txt | grep bar"
这篇关于使用Java 7中的ProcessBuilder构建流程管道的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!