Skip to content

Conversation

naotoj
Copy link
Member

@naotoj naotoj commented Jul 11, 2025

In prior JDK releases, System.console() could return a Console instance even when the JVM was not attached to an interactive terminal. This could lead to confusion, particularly when input was not from a keyboard or output was redirected, such as to or from a file or pipe, especially when using methods like readPassword(). Starting with JDK 25, the default behavior has changed: System.console() now returns null if standard input and/or output is redirected. However, if a JLine-based Console implementation is explicitly specified via the system property -Djdk.console=jdk.internal.le, the previous behavior may still occur.
This PR aims to align the behavior of the JLine-based Console implementation with the default System.console() behavior. The actual code change is a one-liner in JdkConsoleProviderImpl.java; the rest of the changes are adjustments to test cases to reflect the updated behavior. A corresponding CSR has also been drafted.


Progress

  • Change must be properly reviewed (1 review required, with at least 1 Reviewer)
  • Change must not contain extraneous whitespace
  • Change requires CSR request JDK-8361911 to be approved
  • Commit message must refer to an issue

Issues

  • JDK-8361613: System.console() should only be available for interactive terminal (Bug - P4)
  • JDK-8361911: System.console() should only be available for interactive terminal (CSR)

Reviewers

Reviewing

Using git

Checkout this PR locally:
$ git fetch https://git.openjdk.org/jdk.git pull/26273/head:pull/26273
$ git checkout pull/26273

Update a local copy of the PR:
$ git checkout pull/26273
$ git pull https://git.openjdk.org/jdk.git pull/26273/head

Using Skara CLI tools

Checkout this PR locally:
$ git pr checkout 26273

View PR using the GUI difftool:
$ git pr show -t 26273

Using diff file

Download this PR as a diff file:
https://git.openjdk.org/jdk/pull/26273.diff

Using Webrev

Link to Webrev Comment

@bridgekeeper
Copy link

bridgekeeper bot commented Jul 11, 2025

👋 Welcome back naoto! A progress list of the required criteria for merging this PR into master will be added to the body of your pull request. There are additional pull request commands available for use with this pull request.

@openjdk
Copy link

openjdk bot commented Jul 11, 2025

@naotoj This change now passes all automated pre-integration checks.

ℹ️ This project also has non-automated pre-integration requirements. Please see the file CONTRIBUTING.md for details.

After integration, the commit message for the final commit will be:

8361613: System.console() should only be available for interactive terminal

Reviewed-by: jlahoda, smarks, alanb

You can use pull request commands such as /summary, /contributor and /issue to adjust it as needed.

At the time when this comment was updated there had been 481 new commits pushed to the master branch:

As there are no conflicts, your changes will automatically be rebased on top of these commits when integrating. If you prefer to avoid this automatic rebasing, please check the documentation for the /integrate command for further details.

➡️ To integrate this PR with the above commit message to the master branch, type /integrate in a new comment.

@openjdk openjdk bot added csr Pull request needs approved CSR before integration rfr Pull request is ready for review labels Jul 11, 2025
@openjdk
Copy link

openjdk bot commented Jul 11, 2025

@naotoj The following labels will be automatically applied to this pull request:

  • compiler
  • core-libs

When this pull request is ready to be reviewed, an "RFR" email will be sent to the corresponding mailing lists. If you would like to change these labels, use the /label pull request command.

@mlbridge
Copy link

mlbridge bot commented Jul 11, 2025

Webrevs

@ValueSource(strings = {"UTF-8", "ISO-8859-1", "US-ASCII", "foo", ""})
void testDefaultCharset(String stdoutEncoding) throws Exception {
// check "expect" command availability
var expect = Paths.get("/usr/bin/expect");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We only need to check "expect" availability once, so we should move this check to a @BeforeAll static method. It's also more clear that this check is a precondition, and not part of the actual test. Applies to the other locations, but primarily the other parameterized tests.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point. Modified to use @BeforeAll. For ModuleSelectionTest, one of the test is not using expect, so I left it as it is. In addition to that, I removed the @requires condition to allow that test to run on windows.


@ParameterizedTest
@MethodSource("options")
void testWithoutExpect(String opts, String expected) throws Exception {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

expected param looks unused.


@ParameterizedTest
@MethodSource("options")
void testWithExpect(String opts, String expected) throws Exception {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be more clear if these tests were renamed to expectConsoleTest and noConsoleTest or something along those lines.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, using expect command with junit's expect is confusing 🙂. Just renamed those methods using TTY/NonTTY, as"expect/noConsoleTest" reads somewhat odd as it includes the expected results in the test name.

* @requires (os.family == "linux") | (os.family == "mac")
* @library /test/lib
* @build jdk.test.lib.Utils
* jdk.test.lib.JDKToolFinder
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is jdk.test.lib.JDKToolFinder needed?

Copy link
Contributor

@lahodaj lahodaj left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me, although additional review from core libs might be desirable. Thanks!

* with the current Java virtual machine, if any.
*
* @return The system console, if any, otherwise {@code null}.
* @see Console
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The method declaration already links to Console so I don't think we need another link in the "See also" section.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe I missed it, but do we have anything to make it clear that it returns null if either stdin or stdout are redirected?

Copy link

@xuemingshen-oracle xuemingshen-oracle Jul 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we do have wordings like " 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 "If no console device is
available then an invocation of that method will return null" from the very beginning. not very "straightforward" but i think it's clear enough?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think adding @see tag would be more helpful.

As to the spec wording wrt stdin/out, there is another issue filed to make it clearer: JDK-8361972. This PR addresses the implementation part only so that it can be backported to prior LTSes without spec change.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It already links to Console so I don't think think the additional @see is needed. Look at the generated javadoc and see what you think.

@bridgekeeper
Copy link

bridgekeeper bot commented Aug 12, 2025

@naotoj This pull request has been inactive for more than 4 weeks and will be automatically closed if another 4 weeks passes without any activity. To avoid this, simply issue a /touch or /keepalive command to the pull request. Feel free to ask for assistance if you need help with progressing this pull request towards integration!

@naotoj
Copy link
Member Author

naotoj commented Aug 12, 2025

/keepalive

@openjdk
Copy link

openjdk bot commented Aug 12, 2025

@naotoj The pull request is being re-evaluated and the inactivity timeout has been reset.

@openjdk openjdk bot added ready Pull request is ready to be integrated and removed csr Pull request needs approved CSR before integration labels Aug 16, 2025
@mlbridge
Copy link

mlbridge bot commented Aug 21, 2025

Mailing list message from ecki on core-libs-dev:

An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/core-libs-dev/attachments/20250821/88ee07be/attachment-0001.htm>

@stuart-marks
Copy link
Member

ecki wrote:

I know my View of console is always a bit controversial, but having access to /dev/tty despite a program have redirected stdout and stderr would be a feature, not something you actively need to disable.
But I guess writing system tools it not something Java ever was made for :)

[extracted from HTML and edited slightly by me]

There is as usual a bunch of history here. The Java Console class doesn't use /dev/tty at all. In retrospect it might have been preferable for it to have used /dev/tty from the beginning, at least on Unix. However, that's not the case; it's always been a wrapper around the combination of stdin and stdout. Having to support equivalent functions on Windows might have played a part in this design. In any case, since its introduction in JDK 1.6, Console has been based on stdin and stdout and not /dev/tty.

The question then arises as to what should happen if one or both of stdin or stdout have been redirected. Historically, if either were redirected, System.console() would return null, meaning that "the console does not exist." More recently, we introduced the JLine console provider in order to provide line editing. Unfortunately this confused the situation, as it would provide a non-null Console instance in certain cases even if stdin or stdout were redirected, leading to confused and inconsistent behavior. That's what this PR is fixing.

There's still a broader question of, could Console use /dev/tty? Calling System.console() to obtain a Console instance might open /dev/tty directly. This could work even if all of stdin, stdout, and stderr are redirected. And it could provide a full set of features (including no-echo when reading passwords, and line editing) even in this case. This could be quite useful. However, this would be an incompatbile behavior change. Given the history of Console, there are programs out there that assume that it is based around stdin and stdout and mix operations on them. However, it might be possible to create a Console based on /dev/tty, and applications that are prepared for this change could opt into using this implementation. There are also questions about how this would work on Windows.

@naotoj
Copy link
Member Author

naotoj commented Aug 22, 2025

Thanks for the reviews!
/integrate

@openjdk
Copy link

openjdk bot commented Aug 22, 2025

Going to push as commit ae0dac4.
Since your change was applied there have been 509 commits pushed to the master branch:

Your commit was automatically rebased without conflicts.

@openjdk openjdk bot added the integrated Pull request has been integrated label Aug 22, 2025
@openjdk openjdk bot closed this Aug 22, 2025
@openjdk openjdk bot removed ready Pull request is ready to be integrated rfr Pull request is ready for review labels Aug 22, 2025
@openjdk
Copy link

openjdk bot commented Aug 22, 2025

@naotoj Pushed as commit ae0dac4.

💡 You may see a message that your pull request was closed with unmerged commits. This can be safely ignored.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

Successfully merging this pull request may close these issues.

7 participants