-1

I have a command that gives a list of files (like find).

For each of those files i want to do something in Java:

find ./files <some restrictions> | java -jar processor.jar

how can i read from this unnamed / anonymous Pipe?

youseeus
  • 347
  • 7
  • 24
  • possible duplicate of [Command Line Pipe Input in Java](http://stackoverflow.com/questions/1431551/command-line-pipe-input-in-java) – Joe Nov 20 '14 at 14:41
  • System.in is your answer. –  Nov 20 '14 at 14:48

1 Answers1

2

Quite simple really; using a pipe in a shell means that the standard output of the process on the left hand of a pipe is what the standard input of the process on the right hand of the pipe will read.

Therefore, the Java process needs only read stdin, and in Java, this is System.in.


As to using the find command, it's fine, really; but if you use Java 7+ you might consider using a FileVisitor instead and Files.walkFileTree().

fge
  • 119,121
  • 33
  • 254
  • 329
  • but how can i avoid the program try to read from stdin when you run the program without piping? – youseeus Nov 21 '14 at 09:24
  • 1
    Well, you can't use pure Java code to detect whether you read from a pipe; and even if you could I wouldn't recommend it. Why not make your program take options to tell it how it should behave, or recognize arguments (it is customary for instance to use `-` as an argument to tell the program it is to read from stdin)? – fge Nov 21 '14 at 09:57