Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8325190

System.console() behaves weirdly if stdin is redirected

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Duplicate
    • Icon: P4 P4
    • None
    • 23
    • core-libs
    • None
    • aarch64
    • os_x

      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)


            naoto Naoto Sato
            prappo Pavel Rappo
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved: