Java Reference
In-Depth Information
The jdb Debugger
jdb , the Java debugger, is a sophisticated tool that helps you find and fix bugs in Java
programs. You can also use it to understand better what is taking place behind the scenes
in the Java interpreter as a program is running. It has a large number of features, includ-
ing some that might be beyond the expertise of a Java programmer who is new to the
language.
You don't need to use the debugger to debug Java programs. This is fairly obvious, espe-
cially if you've been creating your own Java programs as you read this topic. After the
Java compiler generates an error, the most common response is to load the source code
into an editor, find the line cited in the error message, and try to spot the problem. This
dreaded compile-curse-find-fix cycle is repeated until the program compiles without
complaint.
B
After using this debugging method for a while, you might think that the debugger isn't
necessary to the programming process because it's such a complicated tool to master.
This reasoning makes sense when you're fixing problems that cause compiler errors.
Many of these problems are simple things such as a misplaced semicolon, unmatched {
and } braces, or the use of the wrong type of data as a method argument. However, when
you start looking for logic errors—more subtle bugs that don't stop the program from
compiling and running—a debugger is an invaluable tool.
The Java debugger has two features that are useful when you're searching for a bug that
can't be found by other means: single-step execution and breakpoints. Single-step execu-
tion pauses a Java program after every line of code is executed. Breakpoints are points
where execution of the program will pause. Using the Java debugger, these breakpoints
can be triggered by specific lines of code, method calls, or caught exceptions.
The Java debugger works by running a program using a version of the Java interpreter
over which it has complete control.
Before you use the Java debugger with a program, you will compile the program with the
-g option, which causes extra information to be included in the class file. This informa-
tion greatly aids in debugging. Also you shouldn't use the -O option because its opti-
mization techniques might produce a class file that does not directly correspond with the
program's source code.
Debugging Applications
If you're debugging an application, the jdb tool can be run with a Java class as an argu-
ment. This is shown in the following:
jdb WriteBytes
 
Search WWH ::




Custom Search