Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

Sponsored Links

Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs

Debug with jdb

How do you use this crazy thing?

  • Print
  • Feedback

Q How do you use jdb (included in the JDK 1.2 package) effectively to debug Java programs?

I've tried many times, but I am successful only in loading a class file to jdb; I can't debug it. The help command isn't much use.

AYou ask an interesting question. To be honest, I've never used jdb. I have always used the debugger provided by my IDE environment. So to answer your question I had to do a little research of my own.

It turns out that Sun considers jdb a proof of concept for the Java Debugger API. The Java Debugger API allows us to actually peek into the runtime and debug our code. The jdb is just one implementation of a debugger that uses the API. Compared to the visual debuggers with which I'm familiar (yes, I guess I'm a wimp), it's not the easiest debugger to use -- though it is similar to other command-line debuggers, such as gdb.

Anyhow, on to your question. Before attempting to debug your code, be sure to use the -g option while compiling your classes. This option tells the compiler to include debugging information in your class file.

Let's define a contrived class for testing:

public class TestMe {
  private int int_value;
  private String string_value;
  
  public static void main(String[] args)
  {
    TestMe testMe = new TestMe();
    testMe.setInt_value(1);
    testMe.setString_value("test");
    int integer = testMe.getInt_value();
    String string = testMe.getString_value();
    String toString = testMe.toString();
  }
  public TestMe()
  {
  }
  public int getInt_value()
  {
    return int_value;
  }
  public String getString_value()
  {
    return string_value;
  }
  public void setInt_value(int value)
  {
    int_value = value;
  }
  public void setString_value(String value)
  {
    string_value = value;
  }
  public String toString()
  {
    return "String value: " + string_value + " int value: " + int_value;
  }
  
}


Start the debugger:

> jdb TestMe


You should see:

> Initializing jdb...
> 0xaa:class<TestMe>


Let's take a look at some basic commands. In order to set breakpoints, we need to know the line numbers or the method names of the places where we would like to break. To obtain a list of methods, simply use the methods command:

> methods TestMe
   void main(java.lang.String[])
   void <init>()
   int getInt_value()
   java.lang.String getString_value()
   void setInt_value(int)
   void setString_value(java.lang.String)
   java.lang.String toString()


Setting a breakpoint is simple. Use the following syntax:

stop in <class id>.<method>[<argument_type,...>]


Or:

stop at <class id>:<line>


We should start debugging at the beginning of the main method:

> stop in TestMe.main
   Breakpoint set in javaworld.TestMe.main


Now that we have a breakpoint, we can begin execution. To run up to the breakpoint, simply use the run command:

> run
run javaworld.TestMe
running ...
main[1]
Breakpoint hit: javaworld.TestMe.main (TestMe:10)


At this point, the debugger halts execution at the first line of the main method. Notice that the cursor has changed to reflect the method that we are currently in.

The list command will display the code at the breakpoint. An arrow indicates the spot where the debugger has halted execution.

  • Print
  • Feedback

Resources