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
world% % hexdump input.txt
0000000 6f77 6c72 0064
0000005
% ~/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
world% % hexdump input.txt
0000000 6f77 6c72 0064
0000005
% ~/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-8325191 System.console() can be null if stdin is redirected
-
- Closed
-