What do you do when you need to debug a Java program and your IDE isn't around to help? You turn to jdb, which is the Java Debugger provided by Sun. It's console-based and kind of a throwback to the good old days of programming, but it works, and (when you don't have other options) it's a great tool to have in your programming kit.
With jdb, you can do most of the things you're used to doing in your normal debugger: set break points, evaluate variables, step over and into code, etc. Using jdb will require you to learn some of the jdb commands. To get the most out of your jdb debugging sessions, you'll want to compile your code with the -g switch so that debug information is placed in your class files.
Here's the output of a simple debug session. To start the session, we invoke the jdb command with our application's class name.
/usr/home/reas>jdb DebugTip
Initializing jdb ...
Next, we set a breakpoint in the bar method of our DebugTip class.
> stop in DebugTip.bar
Deferring breakpoint DebugTip.bar.
It will be set after the class is loaded.
Now, we use the run command to start program execution.
> run
run DebugTip
Set uncaught java.lang.Throwable
Set deferred uncaught java.lang.Throwable
>
VM Started: Set deferred breakpoint DebugTip.bar
foo
Breakpoint hit: "thread=main", DebugTip.bar(), line=15 bci=0
15 Date date = new Date();
The jdb has loaded the class and executed statements until our breakpoint is hit. It displays the next line of code to be executed. We use the next command to execute the next statement.
main[1] next
>
Step completed: "thread=main", DebugTip.bar(), line=17 bci=8
17 System.out.println("the time is now: " + date);
After executing the line, jdb stops again. Now we'll use the locals command to display local variables. As you can see by the output, there is only one: date.
main[1] locals
Method arguments:
Local variables:
date = instance of java.util.Date(id=297)
Using the cont command causes jdb to continue executing code until the next breakpoint is reached. Since there are no more breakpoints, execution proceeds until the main method exits.
main[1] cont
> the time is now: Tue Nov 18 11:46:19 EST 2003
bar
The application exited
/usr/home/reas>
Jdb has the features you need for a productive debugging session. If you're used to IDE debuggers, the console interface may be intimidating, but don't let that stop you from learning more about it. When you're trying to identify a problem on an OS with no GUI or without an IDE, jdb is a useful tool to have at your disposal.
There are lots of commands, the most important one being help, which will list all of your available commands and options. Check out the documentation and give it a try with the sample class. It will be time well spent.
import java.util.Date;
public class DebugTip {
public static void main(String args[]) {
DebugTip dt = new DebugTip();
dt.foo();
}
public void foo() {
System.out.println("foo");
bar();
}
private void bar() {
Date date = new Date();
System.out.println("the time is now: " + date);
System.out.println("bar");
}
}






Leave a comment