If System.console() is now a preferred way to interact with STDIN, redirected or otherwise, then the following might be an unexpected behaviour:
% cat Input.java
import java.io.IOException;
public class Input {
public static void main(String[] args) throws IOException {
System.out.print( System.console().readLine() );
}
}
% cat input.txt
hello
% hexdump input.txt
0000000 6568 6c6c 0a6f
0000006
% ~/jdk/build/macosx-aarch64/images/jdk/bin/java -Djdk.console=java.base Input.java < input.txt
Exception in thread "main" java.lang.NullPointerException: Cannot invoke "java.io.Console.readLine()" because the return value of "java.lang.System.console()" is null
at Input.main(Input.java:6)
% cat Input.java
import java.io.IOException;
public class Input {
public static void main(String[] args) throws IOException {
System.out.print( System.console().readLine() );
}
}
% cat input.txt
hello
% hexdump input.txt
0000000 6568 6c6c 0a6f
0000006
% ~/jdk/build/macosx-aarch64/images/jdk/bin/java -Djdk.console=java.base Input.java < input.txt
Exception in thread "main" java.lang.NullPointerException: Cannot invoke "java.io.Console.readLine()" because the return value of "java.lang.System.console()" is null
at Input.main(Input.java:6)
- duplicates
-
JDK-8325190 System.console() behaves weirdly if stdin is redirected
-
- Closed
-