How to read "echo a | java Class" in Java

I need to somehow read "echo a | java Class". I tried

public class Class { public static void main(String[] args) { System.out.println(args[0]); } } 

But that will not work. I can not find another solution. Thanks.

+4
source share
3 answers

Read from stdin , for example, using Scanner :

 Scanner scanner = new Scanner(System.in); String a = scanner.next(); 
+3
source

If you need to use echo a | java Class echo a | java Class , you need to use something like @ João .

However, if you can change your command and want to use args , you need to use xargs :

 echo a | xargs java Class 
+3
source

You can use:

 BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String line; while ((line = br.readLine()) != null) { System.out.println(line); } 

For use:

 echo Hello | java InputTest 
+1
source

All Articles