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.

Share Improve this question edited Mar 8 at 11:23 njlarsson asked Mar 8 at 9:01 njlarssonnjlarsson 2,3931 gold badge19 silver badges31 bronze badges 7
  • 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
 |  Show 2 more comments

1 Answer 1

Reset to default 0

Thanks 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