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

Clarify the condition of System.console() about standard input/output

XMLWordPrintable

    • Icon: CSR CSR
    • Resolution: Approved
    • Icon: P4 P4
    • 26
    • core-libs
    • None
    • behavioral
    • low
    • Hide
      Other JDK implementations could have console providers that return non-null for redirected System.console(), but I expect the possibility is minimal, possibly none. `System.console().isTerminal()` invocation on jshell now returns `true`. It used to return `false` due to the fact that it is the environment in remote process where input/output are redirected.
      Show
      Other JDK implementations could have console providers that return non-null for redirected System.console(), but I expect the possibility is minimal, possibly none. `System.console().isTerminal()` invocation on jshell now returns `true`. It used to return `false` due to the fact that it is the environment in remote process where input/output are redirected.
    • Java API
    • SE

      Summary

      Clarify the conditions under which a Console object exists, particularly when standard input or output has been redirected.

      Problem

      JDK-8361613 restricts the JLine based Console implementation to return a non-null instance from System.console() only when both standard input and output are attached to the terminal. Given the complicated history described in the CSR for that issue, it is desirable to clarify the Console specification regarding the existence of a console when standard input or standard output are not connected to a terminal.

      Solution

      Modify the class description in the java.io.Console class to specify that a console will not exist if either standard input or output has been redirected. Also modify the specification of the isTerminal() method always to return true.

      Specification

      Change the class description of java.io.Console as follows:

        * Methods to access the character-based console device, if any, associated
        * with the current Java virtual machine.
        *
      - * <p> Whether a virtual machine has a console is dependent upon the
      + * <p> Whether a virtual machine's console exists is dependent upon the
        * underlying platform and also upon the manner in which the virtual
        * machine is invoked.  If the virtual machine is started from an
        * interactive command line without redirecting the standard input and
      - * output streams then its console will exist and will typically be
      + * output streams, then its console will generally exist and will be
        * connected to the keyboard and display from which the virtual machine
      - * was launched.  If the virtual machine is started automatically, for
      - * example by a background job scheduler, then it may not
      - * have a console.
      + * was launched. If the standard input or standard output have been
      + * redirected (for example, to a file or to a pipe), or if the virtual
      + * machine was started from a background job scheduler, the console
      + * will not exist.
        * <p>
      - * If this virtual machine has a console then it is represented by a
      - * unique instance of this class which can be obtained by invoking the
      - * {@link java.lang.System#console()} method.  If no console device is
      - * available then an invocation of that method will return {@code null}.
      + * If the console exists, then it is represented by a unique instance of this
      + * class which can be obtained by invoking the {@link System#console()} method.
      + * If the console does not exist, that method will return {@code null}.
        * <p>
        * Read and write operations are synchronized to guarantee the atomic
        * completion of critical operations; therefore invoking methods
        * {@link #readLine()}, {@link #readPassword()}, {@link #format format()},
        * {@link #printf printf()} as well as the read, format and write operations

      Change the method description of isTerminal() as:

             * {@return {@code true} if the {@code Console} instance is a terminal}
             * <p>
      -      * This method returns {@code true} if the console device, associated with the current
      -      * Java virtual machine, is a terminal, typically an interactive command line
      -      * connected to a keyboard and display.
      -      *
      -      * @implNote The default implementation returns the value equivalent to calling
      -      * {@code isatty(stdin/stdout)} on POSIX platforms, or whether standard in/out file
      -      * descriptors are character devices or not on Windows.
      +      * This method always returns {@code true}, since {@link System#console()}
      +      * provides a {@code Console} instance only when both standard input and
      +      * output are unredirected, that is, when running in an interactive terminal.
             *

            naoto Naoto Sato
            naoto Naoto Sato
            Justin Lu, Stuart Marks
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: