CS 547: jdb Tutorial


The goal of this tutorial is to introduce you to the interactive Java debugger, jdb. The documentation for jdb is in ~cs547-1/public/tutorials/java/jdb.html.

A simple Java program called Dates.java is used for this tutorial. Dates.java calculates the number of days between two dates. It almost works, but it has a two syntactic errors and one semantic errors.

PART I: Getting Started

PART II: Correcting Compile-time Errors

The code contains two small syntax errors of a type that you might make when you type in your Java code. Use the messages from the compiler to find the errors in the code and correct them. When the code compiles successfully, the compiler will produce a file called Dates.class.

PART III: Finding the Run-time Error

  1. Run the program using the java interpreter with some inputs, for example: java Dates 1 12 3 4

    The program is supposed to compute the number of days between the first date (1/12) and the second date (3/4). Can you tell there is something wrong?

    Semantic errors which occur at run-time, such as this one, tend to be significantly more difficult to find than compile-time errors. For such errors, jdb is particularly helpful.

  2. Compile Dates.java again with the "-g" option: javac -g Dates.java.

    This option tells the compiler to provide information that jdb will use to display local (stack) variables.

  3. To run your program with jdb type: jdb Dates 1 12 3 4

    At this point, jdb has invoked the Java interpreter, and the interpreter stops before entering main(). You can type: help to see what jdb commands you can use.

    Note that "stop in", "stop at", "run", "cont", "list", "locals" and "print" are especially useful commands.

    Since you have not yet entered the method called "main", the commands that list the code or print the values of program variables will not work yet.

  4. Type:
    stop in Dates.main
    run

    The interpreter will stop after a very short time, just after it enters main.

  5. Type:
    list
    and jdb will print the source code for the lines above and below the first instruction in main.

  6. Type: print args[0], to see the value of the first element in the array called "args".

    One tricky point about jdb is that if you use the step command to execute one instruction at a time, you will step into the instructions for the method called Integer.parseInt. The source code for predefined Java classes is not available to jdb, so jdb cannot list the lines of code or print the values of any variables in that method. Thus, you should set a breakpoint after the arguments are parsed, using the "stop at" command. To figure out where to stop, type: list 33.

  7. Type:
    stop at Dates:35
    cont

    The java interpreter will continue executing until it it is about to execute line 35.

  8. Type: locals

    Continue to examine the program's behavior as it executes. You can set further breakpoints, or use step to execute one instruction at a time. At each breakpoint, use the print or locals command to examine the values of program variables, until you find the error. Note that to set a breakpoint when the method called daysInMonth is called, you can type: stop in Dates.daysInMonth). When that breakpoint is reached, you can list the code or print variable values in that method.

    The error in Dates can be corrected by changing only ONE line. When you have found the error: correct the error, and recompile and execute the code to see if the problem is solved.


vernon@cs.wisc.edu