Skemelio Skemelio - 1 month ago 17
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");
}
}


and

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

One way is to use xargs:

java Test| xargs -I ARGS java Test2 ARGS
Comments