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.
You will find that the compiler prints a number of error messages.
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.
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.
This option tells the compiler to provide information that jdb will use to display local (stack) variables.
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.
The interpreter will stop after a very short time, just after it enters main.
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.
The java interpreter will continue executing until it it is about to execute line 35.
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.