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.
Read from stdin , for example, using Scanner :
stdin
Scanner
Scanner scanner = new Scanner(System.in); String a = scanner.next();
If you need to use echo a | java Class echo a | java Class , you need to use something like @ João .
echo a | java Class
However, if you can change your command and want to use args , you need to use xargs :
args
xargs
echo a | xargs java Class
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