SimpleIO in JEP draft 8323335
Johannes Spangenberg
johannes.spangenberg at hotmail.de
Mon Feb 19 22:25:21 UTC 2024
Hello, I just wanted to share some information someone might find
interesting.
> PPPS. Is it intended that print, println, input use
> System.getConsole() under the hood? I am asking because the console
> potentially uses a different character encoding than PrintStream and
> Scanner.
I am not sure about the encoding, but System.console() may return null
if the input or output are redirected.
java Main > output.txt
If I use this command, System.console() would return null, and the
following application would run into a NullPointerException.
public final class Main {
public static void main(String[] args) {
System.console().readLine("Input: ");
}
}
If you consider this issue, you may always want to use System.in instead
of System.console(). However, if you look at other (non-Java)
applications, there is also another approach. Let's consider the
following command:
cat files.txt | sudo xargs -i cat {} > out 2> err
With this command, sudo will prompt for your password on the Terminal,
although neither STDIN, STDOUT, nor STDERR is connected to the terminal.
You can observe a similar behavior with ssh. I am not aware of this
being properly supported by Java. In Python, this is implemented by the
getpass module
<https://github.com/python/cpython/blob/6cd18c75a41a74cab69ebef0b7def3e48421bdd1/Lib/getpass.py>
for Linux and Windows.
Note: Python has the unfortunate limitation that it does not have a
similar method for non-password-inputs. This causes users to mix
getpass(...) and input(...), causing inconsistent behavior. Here is a
question on StackOverflow <https://stackoverflow.com/q/19661956> about
the topic.
PS: I personally feel like Java could potentially benefit from better
utilities for interactive command line interfaces. However, if this is
something we want to implemented, we shouldn't only think about
beginners. Introducing a tiny separate world just for beginners does not
seem very desirable to me. Anyway, this is just my personal take as a
stranger. I just wrote this email because I wanted to point out the
details above.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/amber-dev/attachments/20240219/718d899f/attachment.htm>
More information about the amber-dev
mailing list