26 September 2006

Up arrow in Java console input on Linux

[edit] Whaddaya know, this is documented here: http://wiki.python.org/jython/ReadlineSetup

So I was running Jython on Linux which has a console application for entering Jython commands. Unfortunately pressing the up-arrow did not recall the last command in the history. Instead, it printed the escape code for up-arrow (^[[A).

I've seen this problem before on Linux. Not sure if it is limited to Java console applications. I'm not even sure why it occurs. Perhaps something to do with buffered input?

Anyway, I did find a solution: JLine. You don't even have to modify a program to get it to work. You can wrap the invocation of the Java main class as follows:


java -cp jline-0.9.9.jar jline.ConsoleRunner MyMainClass


So my command line for Jython became:

java -classpath "jline-0.9.9.jar:jython.jar:$CLASSPATH" jline.ConsoleRunner org.python.util.jython


You can simply edit the Jython startup script to add the JLine jar and insert the jline.ConsoleRunner before the jython main class.

Up-arrow now recalls the last command. Left, right and down arrow work too.

No comments: