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");
    }
}

Interpreting Java This was published in Interpreting Java, check every Tuesday for more stories

Related links

Leave a comment

You must read and type the 6 chars within 0..9 and A..F

* indicates mandatory fields.

Log in


Sign up | Forgot your password?

  • Staff Aussies to pay more for Win 7

    If you are looking to make some money in these troubled times, perhaps importing copies of Windows 7 could be for you. Read more »

    -- posted by Staff

  • Staff Firefox: Greens want it, 3.5rc2 not up to par

    This week's roundup looks at the situation surrounding a campaign to change Outlook HTML renderer, a Greens MP wants to install Firefox but is restricted and all the photos from the iPhone 3GS launch. Read more »

    -- posted by Staff

  • Chris Duckett Microsoft misses the Outlook point

    Ask designers which mail program is the bane of their existence, and you'll find that Outlook tops the list. The reason why the most popular email reader is also the most painful is simple: it uses Word to render HTML emails. Read more »

    -- posted by Chris Duckett

What's on?