8

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.

--Update--

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.

Aleksandar Savkov
  • 2,894
  • 3
  • 24
  • 30

2 Answers2

4

You don't need a pipe in this case.

"grep bla test.txt"

however assuming you need a pipe, you need to use a shell like bash

"/bin/bash", "-c", "grep foo text.txt | grep bar"
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • 3
    I am trying to use the redirectInput and redirectOutput methods in the ProcessBuilder to simulate a pipe. – Aleksandar Savkov Nov 23 '11 at 14:22
  • As far as I can see you can only redirect to a file this way and you cannot add your own types. How exactly are you trying to achieve this? – Peter Lawrey Nov 23 '11 at 14:46
  • 1
    I'm not sure if it's possible. Do you have any idea what the PIPE ([link]http://www.fxfrog.com/docs_www/api/java/lang/ProcessBuilder.Redirect.html#PIPE) type is for? I imagined that it's possible to redirect the output to be the input of another process. – Aleksandar Savkov Nov 23 '11 at 15:03
  • 1
    The Javadoc states this is `Indicates that subprocess I/O will be connected to the current Java process over a pipe.` You have to read/write the stream in the current Java process. i.e. the PIPE is between the process you run and the Java process, not two processes you are running. – Peter Lawrey Nov 23 '11 at 15:19
  • Thank you. I was beginning to realize that it wasn't what I supposed it is. – Aleksandar Savkov Nov 23 '11 at 15:27
  • 1
    I was using a shell, but I wanted to use a more Java-ish way of constructing the pipe. I'm doing it now the way Aaron Digulla suggested. I copied this (http://blog.bensmann.com/piping-between-processes) implementation, works like a charm. – Aleksandar Savkov Nov 23 '11 at 15:32
  • 2
    It's very disappointing that neither the java API nor Apache commons provide a method to chain processes together into a pipeline. I guess not enough people appreciate the elegance of pipelines – Sridhar Sarnobat Sep 22 '17 at 05:39
  • @Sridhar-Sarnobat it's not in the API but you can pass a pipeline of processes to a shell. – Peter Lawrey Sep 22 '17 at 05:54
  • Yeah. I was hoping that java could serve as the glue code so that you could so more creative things. Somehow I also don't trust shells as robust, production-reliable constructs for IPC. I've found some mysterious lack of stdout output when my process was running fine. – Sridhar Sarnobat Sep 22 '17 at 17:16
3

Start a child thread for each Input-/OutputStream pair with a simple copy loop in run().

Aaron Digulla
  • 321,842
  • 108
  • 597
  • 820