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.
You 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.
jdbThe Java Debugger," from Java Developer's Reference, Mike Cohen, et al. (Sams.net Publishing, 1996)
I use Linux maybe you tooBy Anonymous on February 15, 2010, 8:17 amThe same thing happens to me too. I know a workaround. Just try setting a breakpoint at main first. Then run it. It will then stop at main and you can set more...
Reply | Read entire comment
Initializing jdb... then nothingBy Anonymous on September 14, 2009, 8:36 pmwhen I start jdb with a class name, all i see is initializing jdb and nothing else appears. Can anyone tell me why this might be happening?
Reply | Read entire comment
Debug the programBy Rakesh Kumar on April 28, 2009, 12:41 pmHow we can debug the java program on JCreater. like if we want to debug program in C++ language then we have to use F8 key. in the same way what we have to use...
Reply | Read entire comment
JDB help fileBy Anonymous on October 12, 2008, 6:08 amshort and sweet guide!!!
Reply | Read entire comment
View all comments