admin管理员组文章数量:1405576
I’m working on a program that uses java.io.Console.readLine
to get input from the user, and to my surprise I noticed that on my Mac, it works to press Up Arrow to recall the previous command (like in a normal shell). It doesn’t work on any of my Linux installations, however.
On the Mac, I’m running Java 23 [OpenJDK Runtime Environment Homebrew (build 23.0.1) to be exact], and the newest I have on Linux is 21 [OpenJDK Runtime Environment (build 21.0.6+7-Ubuntu-122.04.1)].
What I’m wondering is: Is this a difference between Java 21 and 23, or is it a macOS versus Linux difference? If it’s Mac vs Linux, I’m going to integrate GNU Readline or something into the Linux version of my code, but I’m not going to bother with that if it’s just a matter of waiting for a newer package to appear in Linux package management.
By request: here is a sample program:
public class ConsTest {
public static void main(String[] args) {
java.io.Console cons = System.console();
while (true) {
cons.readLine("prompt> ");
}
}
}
Compile the program with javac ConsTest.java
in a terminal window and then start it with java ConsTest
. Then type whatever
at the prompt and press Enter. Then, if you press Up Arrow, the question is if whatever
reappears or if you just get ^[[A
.
I’m working on a program that uses java.io.Console.readLine
to get input from the user, and to my surprise I noticed that on my Mac, it works to press Up Arrow to recall the previous command (like in a normal shell). It doesn’t work on any of my Linux installations, however.
On the Mac, I’m running Java 23 [OpenJDK Runtime Environment Homebrew (build 23.0.1) to be exact], and the newest I have on Linux is 21 [OpenJDK Runtime Environment (build 21.0.6+7-Ubuntu-122.04.1)].
What I’m wondering is: Is this a difference between Java 21 and 23, or is it a macOS versus Linux difference? If it’s Mac vs Linux, I’m going to integrate GNU Readline or something into the Linux version of my code, but I’m not going to bother with that if it’s just a matter of waiting for a newer package to appear in Linux package management.
By request: here is a sample program:
public class ConsTest {
public static void main(String[] args) {
java.io.Console cons = System.console();
while (true) {
cons.readLine("prompt> ");
}
}
}
Compile the program with javac ConsTest.java
in a terminal window and then start it with java ConsTest
. Then type whatever
at the prompt and press Enter. Then, if you press Up Arrow, the question is if whatever
reappears or if you just get ^[[A
.
- It would be nice if you provide a sample program that can be used to reproduce the issue on Mac and Linux. You can edit your question accordingly … – tquadrat Commented Mar 8 at 10:15
- Okay, I added an example program. – njlarsson Commented Mar 8 at 10:44
- Hello njlarsson, I don't get "whatever" or "^[[A", I guess it has more to do with the IDE you use than with Linux/Mac, I use NetBeans. – Marce Puente Commented Mar 8 at 11:05
- 1 Kubuntu 24.04, Java JDK 23, now get "whatever"!!... – Marce Puente Commented Mar 8 at 11:34
- 1 Works for me in Debian Bookworm, Java 23 – g00se Commented Mar 8 at 12:29
1 Answer
Reset to default 0Thanks for the comments that verified that the Up Arrow line editing worked in Linux for JDK 23. I dug a little deeper and found this, which, if I interpret it correctly, that the behavior was introduced in JDK 22.
本文标签: linuxWhere does Up Arrow work in Java ConsoleStack Overflow
版权声明:本文标题:linux - Where does Up Arrow work in Java Console? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744899434a2631257.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论