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.
Note that there's a "." at the end of the above command.
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.
When the code compiles successfully, the compiler will produce a file called Dates.class.
(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.
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.
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).
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?