Arenim
Arenim

Reputation: 4237

java nice console input

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?

Upvotes: 2

Views: 920

Answers (3)

Arenim
Arenim

Reputation: 4237

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.

Upvotes: 2

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();
}

  }

Upvotes: -1

mtk
mtk

Reputation: 13709

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.

Upvotes: 2

Related Questions