Let's have a simple Java application where I have the following code to scan for user input:
Scaner scan = new Scanner(System.in);
System.out.print("Enter the value: ");
int value = scan.nextInt();
System.out.println("You entered: " + value);
When I manually run it, as expected I see what I type, a sample run would be:
Enter the value: 3
You entered: 3
However, when I have a file with content
3
And I want to redirect the programs stdout to a file by this:
java Test < INPUT > OUTPUT
All I get is in cat OUTPUT
:
Enter the value: You entered: 3
As the values inputted are not shown on the stdout, which is correct, they are not on stdout, I know, I see them because I manually type them to my terminal. But how can I make them also seen when I redirect the stdout like that?
Edit:
I can see that code is making a read(0,
syscall.
$ (strace -ff -e trace=read -e read 2>&1 java Test < INPUT) | grep "read(0,"
[pid 7297] read(0, "3\n", 8192) = 2
Is there a better way to intercept it?
Edit 2:
I mean, the OUTPUT should be the following when I run it with java < INPUT > OUTPUT
Enter the value: 3
You entered: 3