Skemelio Skemelio - 1 year ago 89
Bash Question

How to pipe input from a Java program to another using Bourne Shell

I have two simple Java programs and I want to pipe the result of the "Test" to the "Test2".

public class Test{
public static void main(String args[]){
System.out.println("Hello from Test");


public class Test2{
public static void main(String args[]){
System.out.printf("Program Test piped me \"%s\"",args[0]);

After I compiled both of .java files I tried to run the pipe command from terminal

java Test | java Test2
, but I get an ArrayIndexOutOfBoundsException which means that the args array is not initialized?

How can the Test2 application take the outputstream value that Test.main() produced through piping?

Answer Source

One way is to use xargs:

java Test| xargs -I ARGS java Test2 ARGS
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download