Piping Input using Java using command line
Asked Answered
B

4

6
public class ReadInput {
    public static void main(String[] args) throws IOException {
    BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
        String x = null;  
        while( (x = input.readLine()) != null ) {    
            System.out.println(x); 
        }    
    }
}  

I am able to run this code from the command line by typing 'java ReadInput < input.txt' but not by typing the input directly like 'java ReadInput hello'. When I type 'java ReadInput hello' I seem to get stuck in an infinite loop for some reason. Shouldn't it just work in the same way as typing 'java ReadInput < input.txt' but instead just reprint 'hello'?

Budding answered 7/5, 2011 at 1:16 Comment(0)
B
7

Arguments given on the program's command line don't go to System.in, they go in the args array. You could use something like echo hello | java ReadInput to run the program, or you could modify the program to to look at its args array and treat it as input. (If you use the latter option, you'd probably want to fall back to reading from System.in if there's nothing in args.)

Behead answered 7/5, 2011 at 1:29 Comment(0)
F
5

Both of the other two answers above are partially correct, but partially misleading.

Using the following syntax...

java ReadInput < input.txt

...the actual command that is run on the java binary is:

java ReadInput

The operating system shell interprets the < sign, and sends the contents of input.txt to the Standard Input Stream (System.in).

When you call System.in.readLine(), the code checks whether there is a line of input available from the Standard Input Stream. When you piped in a file, this means it takes the next line of the file (as if the OS were taking the contents of the file and typing them in at the prompt). When you run the program without piping a file, it will wait until you, the user, provide it with a line of input from the shell and press the return key.

Command-line arguments to the JVM work differently, and do not use the Standard Input Stream (System.in). Ex:

java ReadInput "Input number one" "Input number two"

In this case, you are passing in two command-line arguments. These properties will be available through the args array:

   public static void main(String[] args) throws IOException {
          System.out.println(args.length); //3
          System.out.println(args[0]); //"ReadInput"
          System.out.println(args[1]); //"Input number one"
          System.out.println(args[2]); //"Input number two"
   }

With the code that you provided, the program will end when the result of a readLine() call returns null. I believe you may be able to just push enter at the prompt to send a blank line, and end the program. If not, you may have to fix the program to check for an empty string (input.readLine().equals("")).

Hope this helps.

Florio answered 10/5, 2011 at 1:40 Comment(1)
On *nix you can terminate the input with Ctrl-D (the EOF code which terminates the stdin pipe).Civet
D
3

When you type java ReadInput hello you have not (yet) provided any input to System.in so the program execution blocks until there is something to read. You might notice that the string "hello" is passed into the args parameter to the main() method.

If you're using a debugger (which I strongly recommend), place a breakpoint at the first line of main(). You'll notice that, when you run your program as java ReadInput hello that args[0] is "hello".

If you're not using a debugger, you can use System.out.println() (this is how beginners frequently debug their code). Add this line of code as the first line of main():

if (args.length > 0) System.out.println(args[0]);
Darrondarrow answered 7/5, 2011 at 1:26 Comment(2)
If this is the case do you know how I would pipe in some input using only the command line? I understand what you mean about "hello" is really args[0], but what if I want to test my code without having to specify a file to read from?Budding
@mephisto_, see the example in my answer of using echo to pipe input to the program.Behead
P
1

Terminate the standard input with

Ctrl-D  :  Linux/Unix 
Ctrl-Z (or F6) and "Enter" : Windows.

Or set a special EOF symbol youself.

Publicly answered 22/1, 2015 at 7:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.