NetBeans-SCSNI

6.2-Describe how to debug a local (desktop) application, including setting breakpoints and stepping through code.

Within the NetBeans IDE, you can debug by setting breakpoints and watches in your code, then running your application in the debugger. You can execute your code one line at a time and examine the state of your application in order to discover any problems. When you start a debugging session, all of the relevant debugger windows appear automatically at the bottom of your screen. You can debug an entire project, any executable class, and any JUnit tests. The IDE also lets you debug applications that are running on a remote machine by attaching the debugger to the application process.

A breakpoint is a flag in the source code that tells the debugger to stop execution of the program. When your program stops on a breakpoint, you can perform actions like examining the value of variables and single-stepping through your program. The Source Editor indicates a breakpoint by highlighting the line in red and placing an annotation in the left margin.

Except for line breakpoints, all Java breakpoints are defined globally for all IDE projects. For example, if you set a breakpoint on a class, the IDE will stop execution every time it encounters that class during a debugging session regardless of what project you are debugging.

To set a line breakpoint, click on the left margin of the line in the Source Editor. Figure 6 shows two breakpoints; a line breakpoint is denoted by a red square, and a field breakpoint is displayed as a triangle.

Breakpoints in Source Editor
Figure 6. Breakpoints in Source Editor
 

You can customize a breakpoint by right-clicking on the breakpoint icon and choosing Breakpoint > Customize. The Customizer dialog enables you to set conditions and actions on the breakpoint. You can set other types of breakpoints as well, such as the following:

  • Method -- Program execution stops every time the method is executed.
  • Exception -- You can break whenever a specific exception is caught, whenever a specific exception is not handled in the source code or whenever any exception is encountered regardless of whether the program handles the exception or not.
  • Variable -- You can stop execution of your program whenever a variable in a specific class is accessed (for example, the method was called with the variable as an argument) or modified.
  • Thread -- You can break program execution whenever a thread starts, stops, or both.
  • Class -- You can break when the class is loaded into the virtual machine, unloaded from the virtual machine, or both.

To run the debugger tool, click the Debug Main Project icon ( Debug icon ) in the main tool bar, or choose Run > Debug Main Project from the main menu. For a quick demonstration of the debugger tool in action, see Demo of JDK Debugging.

Working with Debugging

The following table shows how to debug a project.


Prepare to debug

  • Attach source code for the JDK and each of your JAR files.
  • Set breakpoints in your code.
  • Set watches in your code.
  • If you are working in a free-form project, create a debug target for your Ant script.


Begin a debugging session

  • Start a local debugging session.
  • Debug a single file.
  • Start a remote debugging session.


Monitor your code

  1. Step through your code.
  2. View debugging information
  3. Fix any errors and continue debugging.


Finish the debugging session

  • To finish the current debugging session.
  • To finish one of several debugging sessions.

 Debugging Tasks: Quick Reference

This topic describes common tasks you can perform when debugging a project. For more detailed information, click the links in the right column.

To perform this task

Follow these steps

Start a local debugging session.

  • To debug the main project, choose Run > Debug Main Project (Ctrl-F5).
  • To debug any individual project, right-click the project and choose Debug Project.

Start a remote debugging session.

  1. On the computer where the program is located, start the program in debugging mode.
  2. On the computer where the IDE is running, open the projects that contain the source for the program.
  3. Choose Run > Attach Debugger.
  4. Select the debugger, specify the connector type, enter any additional required process information and click OK.

Debug a single file.

  1. Select any runnable file in the Projects window
  2. Choose Run > Run File > Debug file_name.

Finish a debugging session.

  • To finish the current session, choose Run > Finish Debugger Session (Shift-F5).
  • To finish any session, open the Sessions window (Alt-Shift-6), right-click the session, and choose Finish.

Set a line breakpoint.

  • In the Source Editor, click in the left margin next to the desired line.

Set any type of breakpoint.

  1. In the Source Editor, select the element of code on which you want to set a breakpoint.
  2. Choose Run > New Breakpoint (Ctrl-Shift-F8).
  3. In the New Breakpoint dialog box, select the breakpoint type, set any required options, and click OK.

Modify breakpoint properties.

  1. Open the Breakpoints window (Alt-Shift-5).
  2. Right-click the breakpoint, and choose Customize.
  3. Change any required settings and actions and click OK.

Set a watch.

  • In the Source Editor, right-click a variable or expression and choose New Watch (Ctrl-Shift-F7).

Set a fixed watch.

  1. Open the Local Variables window (Alt-Shift-1) or Watches window (Alt-Shift-2).
  2. Right-click a variable or watch and choose Create Fixed Watch.

Modify a watch.

  1. Open the Watches window (Alt-Shift-2).
  2. Right-click the watch, and choose Customize.

Suspend and resume a thread.

  1. Open the Threads window (Alt-Shift-7).
  2. Right-click the thread and choose Suspend or Resume.

Step into JDK classes.

  1. Open the Sources window (Alt-Shift-8).
  2. Select the checkbox for the archive file or directory containing the JDK sources.

Manage which classes the debugger steps into.

  • Open the Sources window (Alt-Shift-8).
  • Uncheck the checkbox for any source directories you do not want the debugger to step into.

Pop the most recent call from the call stack.

Pop multiple calls from the call stack.

  1. Open the Call Stack window (Alt-Shift-3).
  2. Right-click the call that you want to remain at the top of the call stack.
  3. Choose Pop to Here.

Browse information for calls on the call stack.

  • To move one level away from the main routine, choose Run > Stack > Make Callee Current (Ctrl-Alt-up arrow).
  • To move one level toward the main routine, choose Run > Stack > Make Caller Current (Ctrl-Alt-down arrow).
  • To make a call current, double-click the call in the Call Stack window.

 


-- Wagner R. Santos