Browse DevX
Sign up for e-mail newsletters from DevX


Use the Java Debugger to Determine the Causes of Errors : Page 2

Although the Java Virtual Machine frees programmers from many low-level tasks like garbage collection, it can't protect them from mistakes in design and logic. This article explores the Java Debugger, which you can use to trace through a running program and determine the causes of any errors.




Building the Right Environment to Support AI, Machine Learning and Deep Learning

A Sample Debugging Session
Listing 1 presents a simple Java program that defines a typical student object. The student can receive money and buy pizza, among other methods. This class must be compiled like this if you want to debug it:

javac -g students.java

The -g flag tells the Java compiler to include additional information in the resulting .class file, which aids in the debugging process (see Figure 1). Remember, to recompile your Java class without debugging information when releasing it for others to execute. That way, you minimize the file size of your classes. Plus, this does not adversely affect the execution of the class within the Java Virtual Machine in any way.

Figure 1: Compiling a Class with the -g Flag and Executing It Normally

However, you can now run the students program through the debugger and get quite a different scenario by using the following command:

jdb students

Your IDE will advise you that JDB has initialized and then prompt you as to what to do next. The class does not automatically run, like it does when the Java interpreter executes it. You have 33 commands that you can enter at this prompt. Table 1 lists the most important.

Table 1. JDB Commands and Their Descriptions
This table names and describes the most important JDB commands available.
help (or ?) Displays the commands supported by JDB
exit (or quit) Terminates a debugging session
memory Displays both the amount of memory used and the amount of memory available to the Java Virtual Machine
!! Repeats the last command
load Loads a class into the debugger
run Starts execution of a class that has been loaded
classes Lists classes currently known to JDB
dump Displays all information relating to a thread, variable, class, field, or argument
list Displays source code lines (from the original Java source code file)
locals Prints all current local variables
print Displays information for a thread, variable, class, field, or argument
methods Lists the public methods of the current class
stop in class:method Sets a breakpoint at the first bytecode of a specific method in a specific class (Execution will stop as soon as this method is called.)
stop at class:line Sets a breakpoint at a line in the class' source code (Execution will stop as soon as this line is reached.)
clear Removes all existing breakpoints
clear class:line Clears a specific breakpoint
where Displays the call stack, tracing the execution path from class to class
step Executes the current line of code and stops immediately afterwards
next Executes the current line of code completely and stops again (Method calls are not single-stepped.)
cont Continues program execution, running until the next breakpoint or exception, or until the program terminates

Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date