Home > Articles > Programming > Java

Debugging Java Applications

  • Print
  • + Share This
The Java NetBeans IDE has plenty of support for troubleshooting and optimizing applications. Its built-in debugging support allows you to step through your code incrementally and monitor aspects of the running application, such as values of variables, the current sequence of method calls, the status of different threads, and the creation of objects. In this sample book chapter, you'll learn about NetBeans debugging features, so you can get your code working right as fast as possible.
This chapter is from the book
  • Starting a Debugging Session
  • Attaching the Debugger to a Running Application
  • Starting the Debugger Outside of the Project's Main Class
  • Stepping Through Code
  • Setting Breakpoints
  • Managing Breakpoints
  • Customizing Breakpoint Behavior
  • Monitoring Variables and Expressions
  • Backing Up from a Method to Its Call
  • Monitoring and Controlling Execution of Threads
  • Fixing Code During a Debugging Session
  • Viewing Multiple Debugger Windows Simultaneously

NETBEANS IDE PROVIDES A RICH ENVIRONMENT for troubleshooting and optimizing your applications. Built-in debugging support allows you to step through your code incrementally and monitor aspects of the running application, such as values of variables, the current sequence of method calls, the status of different threads, and the creation of objects.

When using the IDE's debugger, there is no reason for you to litter your code with System.out.println statements to diagnose any problems that occur in your application. Instead, you can use the debugger to designate points of interest in your code with breakpoints (which are stored in the IDE, not in your code), pause your program at those breakpoints, and use the various debugging windows to evaluate the state of the running program.

In addition, you can change code while debugging and dynamically reload the class in the debugger without having to restart the debugging session.

Following are some of the things that you can do within the IDE's debugger:

  • Step through application code line by line.
  • Step through JDK source code.
  • Execute specific chunks of code (using breakpoints as delimiters).
  • Suspend execution when a condition that you have specified is met (such as when an iterator reaches a certain value).
  • Suspend execution at an exception, either at the line of code that causes the exception or in the exception itself.
  • Track the value of a variable or expression.
  • Track the object referenced by a variable (fixed watch).
  • Fix code on the fly and continue the debugging session with the Apply Code Changes command.
  • Suspend threads individually or collectively.
  • Step back to the beginning of a previously called method (pop a call) in the current call stack.
  • Run multiple debugging sessions at the same time. For example, you might need this capability to debug a client-server application.

Starting a Debugging Session

The simplest way to start using the debugger is to choose Run | Step Into. The program counter (marked by green background highlighting and the arrowkey.gif icon, as shown in Figure 5-1) stops one line into the main method of your main project.

05fig01.gif

Figure 5-1 A suspended program with the green program counter showing the next line to be executed

You can then step through your code incrementally with any of the Step commands to observe the program flow and monitor the evolving values of variables in the Local Variables window. See Stepping Through Code later in this chapter for a description of all of the Step commands and the ensuing topics for information on how to take advantage of the debugger's capabilities.

More likely, you will want to start stepping through code at some point after the start of the main method. In this case, you can specify some point in the program where you want to suspend the debugged execution initially and then start the debugger. To do this:

  1. Set a line breakpoint in your main project by opening a class in the Source Editor and clicking in the left margin next to the line where you want to set the breakpoint (or by pressing Ctrl-F8).

    You know that the breakpoint has been set when the pink breakpoint.gif glyph appears in the margin and the line has pink background highlighting (as shown in Figure 5-2).

    05fig02.gif

    Figure 5-2 Code in the Source Editor with a debugger breakpoint set

  2. Press F5 to start debugging the main project.

When the execution of the program stops at the breakpoint (which you can see when the pink breakpoint highlight is replaced by the green highlight of the program counter), you can step through the code line by line while viewing the status of variables, threads, and other information.

See the ensuing topics for details on stepping and viewing program information.

  • + Share This
  • 🔖 Save To Your Account