Tutorial for jdb

The goal of this tutorial is to introduce you to the interactive Java debugger known as jdb. You will find this to be a useful tool in debugging your Java code. A graphical interface to this debugger is provided on Unix systems by the ddd tool.

You will use a program called Dates.java for this exercise. Dates.java is a simple program for calculating the number of days between two dates. It almost works, but needs your help in getting there.

PART I: Getting Started

  1. Login to a Unix machine and open two Unix windows -- one for editing the java program, and another for compiling and running the program.

PART II: Correcting Compile-time Errors

  1. Make a directory for the Dates.java program: mkdir test

  2. Connect to your test directory in both Unix windows: cd test

  3. Copy the Dates.java program for this exercise by typing:

    cp ~vernon/public/html/cs367/tutorials/Dates.java .

    Note that there's a "." at the end of the above command.

  4. Compile the program: javac Dates.java

    You will find that javac cannot compile the program. The code contains two small syntax errors of a type that you might make when you type in your Java programs.

  5. Use the messages from the compiler to locate the errors in the code and correct them.

When the code compiles successfully, the compiler will produce a file called Dates.class.

PART III: run-time error

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

    (Remember that the .class file extension on Dates.class is assumed. If you accidentally type "java Dates.class" it will look for a file called "Dates.class.class".)

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

    This type of error, which occurs at run-time, tends to be significantly more tricky to correct than compile-time errors. It is for this type of error that the debugger jdb is particularly helpful.

  2. Recompile your program with the "-g" option, which will tell the compiler to provide information that jdb can use to display local (stack) variables:
    javac -g Dates.java

  3. Run your program by having jdb start the Java interpreter: jdb Dates 1 12 3 4

    At this point, jdb has invoked the Java interpreter, the Dates class is loaded, and the interpreter stops before enterning main(). Try typing: 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. Give the command stop in Dates.main and then run. The interpreter will continue executing for a very short time until just after it enters main().

    Now you can type list to see the source code for the instructions that are about to execute, or you can type print args to see the value of the variable 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 command stop at Dates:31 and then type cont to let the interpreter continue executing til it again reaches a breakpoint (i.e., until it is about to execute line 31).

  5. You should continue to examine the program's behavior as it executes by setting further breakpoints, or using 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 isolate the error. Note that when the method called daysInMonth is called, jdb can stop at a breakpoint in that method (e.g., if you say stop in Dates.daysInMonth) and it 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 think you have found the error: copy the file to another file in case you need to use it later, correct the error, and recompile and execute it to see if the problem is solved.

Congratulations! You're done with the jdb exercise.

One more experiment you might be interested in is the following. You may think that the compiled code is named Dates.class because the input file is named Dates.java. In the Dates.java file, there is a line that starts "class Dates {". Change it to "class DATES {" and recompile using javac. What new file was created?