2

I'm trying to read a line from console in java. Here is my code:

System.console().readLine("shell $ ");

This works, but I want to use beloved linux features like arrows to move cursor (not inserting ^[[C like now), up arrow should address previous command, tab should autocomplete something (this is not nesessary, but useful, i will be satisfied with correct arrows behaviour).

And -- no, I want to use "pure java" solution, not JNI wrapper for libreadline.

Is there some functionality in JDK itself or some frameworks which provide such functions?

2
  • 1
    It might be overkill, but JCurses can do a lot of this (and more really) Commented Feb 27, 2013 at 17:35
  • Also something called the Enigma Console library Commented Feb 27, 2013 at 17:36

3 Answers 3

2

There is no such in-built functionality in java. For being able to do so i.e. creating a console able to do things like cycling through previous commands, moving around with arrow key, there is a need to write a action handler for such keys which is not possible in java (as per the in built functionality) because the input is buffered in java and it get's flushed as soon as you press Enter.

You might be able to do so in JNI i.e. Java Native Language. As far as I know, you can look at java console api, more details on this blog.

Sign up to request clarification or add additional context in comments.

Comments

2

I have to respond myself.

I didn't found really pure java library to handle it, but it can be written for *nix with no doubt (System.in is NOT buffered in fact).

I'm using jline2 (https://github.com/jline/jline2) https://github.com/jline/jline2now because it small enogh and requires binary additions only to support windows; I've tested it's jars and they're working without recompilation on any *nix and win32 OS I've ever found.

So, answer is: This is possible, but noone have written such 100% pure library.

Comments

-1

Sunilkumkar from vmoksha

   public class ReadConsoleSystem {
    public static void main(String[] a``rgs) { 
System.out.println("Enter something here : "); 
try{
    BufferedReader bufferRead = new 
     BufferedReader(new      InputStreamReader(System.in));
    String s = bufferRead.readLine(); 
    System.out.println(s);
}
catch(IOException e)
{
    e.printStackTrace();
}

  }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.