8

When I run jdb in bash the arrow keys produce weird garbage:

up: ^[[A
down: ^[[B
left: ^[[D
right: ^[[C

So I can't use the command history, or correct a spelling mistake, because I can't navigate the text at all, which is very annoying. Is there a solution to this?

Java version info:

"1.6.0_24"                                                                             
OpenJDK Runtime Environment (IcedTea6 1.11.5) (fedora-68.1.11.5.fc16-x86_64)                        
OpenJDK 64-Bit Server VM (build 20.0-b12, mixed mode)

Bash version info:

GNU bash, version 4.2.28(1)-release (x86_64-redhat-linux-gnu)
bug
  • 515
  • 1
  • 5
  • 17

3 Answers3

10

Have you tried rlwrap? You can install rlwrap and run

rlwrap jdb MyMainClass <args>

instead of just

jdb MyMainClass <args>
RBF06
  • 2,013
  • 2
  • 21
  • 20
4

Have you tried running JLine with JDB ?

Can I use JLine as the input handler for jdb (the java debugger)?

Yes. Try running:

java jline.ConsoleRunner com.sun.tools.example.debug.tty.TTY args

JLine gives you cursor interaction and command line history.

Brian Agnew
  • 268,207
  • 37
  • 334
  • 440
  • Doesn't work for me, I get the error: Could not find the main class: jline.ConsoleRunner. (I'm using JLine v0.9.94 on Fedora 16.) – bug Dec 14 '12 at 11:54
  • 1
    After adding jline.jar and tools.jar to my classpath, I can now use JLine when e.g. debugging a JUnit test case like this: java -classpath $CLASSPATH jline.ConsoleRunner com.sun.tools.example.debug.tty.TTY -classpath $CLASSPATH junit.textui.TestRunner TestMyExample – bug Dec 14 '12 at 16:22
  • 1
    Did the TTY class change names or move to a different namespace? Today, on the OpenJDK 7.u51_2.4.4-1 and on JDK 1.7.0_45 I get `Error: Could not find or load main class com.sun.tools.example.debug.tty.TTY` when I just run the simple command `java com.sun.tools.example.debug.tty.TTY`. – David Grayson Jan 26 '14 at 22:17
  • 1
    To answer my own question: You need to make sure "tools.jar", which comes with the JDK, is on your classpath. That class was not renamed and you can see a copy of its source code here: https://github.com/openjdk-mirror/jdk7u-jdk/blob/f4d80957e89a19a29bb9f9807d2a28351ed7f7df/src/share/classes/com/sun/tools/example/debug/tty/TTY.java – David Grayson Jan 27 '14 at 03:00
3

Following up on Brian's suggestion of using JLine, this worked reasonably well.

I finally could use up/down to browse through command-history, but it had some shortcomings, such as no support for ALT+DEL (to delete last word), CTRL+LEFT/RIGHT (to move cursor one word back/forward) and CTRL+R (reverse search past commands).

I then learnt that such facilities are being offered by JLine2, so I put some time into trying that out instead.

It was quite a painful journey, as I'm on OpenSUSE 12.3 presently, I won't bore you with all the details, but I will outline them, in-case you're really keen on this and find yourself having to follow a similar journey:

  • JLine2 is only offered with source (atleast on OS12.3), so no easy rpm install
  • It requires maven to build it (which the official OpenSUSE 12.3 repositories don't offer, but thankfully there is an un-official rpm someone made for it)
  • Since I'm building it from work, Maven had proxy issues, so I needed to provide my proxy details in the "/usr/share/maven2/conf/settings.xml" file.
  • Then when I typed "mvn install", there were problems with a few maven project dependencies that I had to download+install manually ("maven-scm-api-1.5.jar", "jansi-1.11.jar" and "bsh-2.0b4.jar")
  • After this, it finally built, but had problems running, but I solved these via a tweak mentioned here

After this, it worked reasonably ok, I get most of the perks I was missing out on with JLine1, but unfortunately, jdb's "> " prompt seems to interfere with the movement of the cursor during the CTRL+LEFT/RIGHT actions, which is a shame.

For now, I get around this by typing CTRL+P followed by CTRL+N (this seems to clear the "> " prompt and make everything work nicely)

SIDE-NOTE: I found it painful to type a big long command to run jdb with jline, so I found it nicer to run jline2+jdb via a bash-script such as this:

#!/bin/sh
#GI: This is a version of jdb that runs via jline, so that you can up/down through command history

# JLINE V1.0 METHOD
# =================
#/usr/local/jdk1.6.0_29/bin/java -classpath /usr/share/java/jline.jar:/usr/local/jdk1.6.0_29/lib/tools.jar jline.ConsoleRunner com.sun.tools.example.debug.tty.TTY $*

# JLINE V2.* METHOD
# =================
/usr/local/jdk1.6.0_29/bin/java -classpath /usr/share/java/jline.jar:/usr/local/jdk1.6.0_29/lib/tools.jar jline.console.internal.ConsoleRunner com.sun.tools.example.debug.tty.TTY $*

For example, you could then run your program via jline+jdb with a much simpler:

jjdb.sh -classpath './*':'../lib/*' myprogram.MyMainClass

Ok, hope that helps anyone that's craving a bit more. If you need more detail on any of my journey, let me know, I keep fairly detailed logs, but just didn't want to burden the reader with too much detail (unless it turns out you really needed it!:))

Gurce
  • 592
  • 7
  • 18