Dax Durax
Dax Durax

Reputation: 1657

eclipse pipe stdout to another project

I have two projects in eclipse;

Project A is constantly streaming (currently to STDOUT) output (each new value streamed is newline delimited)

Project B I want to take in this, and do some stuff with it etc,

The only way I have found to do this is by writing to a file; I would prefer to avoid this as there is a ridiculous amount of data (at least for my resources), and most of it will end up being tossed.

Any advice appreciated, thanks!

Upvotes: 0

Views: 457

Answers (1)

iddo
iddo

Reputation: 182

There are several methods of approaching this:

  1. Using TCP socket - not that difficult to achieve and gives you the ability to work remotely (run each program on a different computer).
  2. Create a segmented file by program A and consume them with program B - The nuances are a bit tricky but overall a robust technique if it suits your needs
  3. Use OS pipes - the easiest method given your current situation

I'll demonstrate option 3.

ProgramA.java:

for (int i=0; i<10; i++) {
    System.out.println(i);
}

ProgramB.java:

java.io.BufferedReader br = new java.io.BufferedReader(new java.io.InputStreamReader(System.in));
String input;
while((input=br.readLine())!=null){
    System.out.println("----" + input);
}

After which you can do this at the command line:

java -cp <program_A_classpath> ProgramA | java -cp <program_B_classpath> ProgramB

Output will be:

----0
----1
----2
----3
----4
----5
----6
----7
----8
----9

Upvotes: 1

Related Questions