|
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.
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 (
) 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
|
- Step through your code.
- View debugging information
- 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.
|
- On the computer
where the program is located, start the program in debugging mode.
- On the computer
where the IDE is running, open the projects that contain the source for
the program.
- Choose Run > Attach Debugger.
- Select the
debugger, specify the connector type, enter any additional required
process information and click OK.
|
|
Debug a single file.
|
- Select any runnable
file in the Projects window
- 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.
|
- In the Source
Editor, select the element of code on which you want to set a
breakpoint.
- Choose Run > New Breakpoint (Ctrl-Shift-F8).
- In the New
Breakpoint dialog box, select the breakpoint type, set any required
options, and click OK.
|
|
Modify breakpoint properties.
|
- Open the
Breakpoints window (Alt-Shift-5).
- Right-click the
breakpoint, and choose Customize.
- 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.
|
- Open the Local
Variables window (Alt-Shift-1) or Watches window (Alt-Shift-2).
- Right-click a
variable or watch and choose Create Fixed
Watch.
|
|
Modify a watch.
|
- Open the Watches
window (Alt-Shift-2).
- Right-click the
watch, and choose Customize.
|
|
Suspend and resume a thread.
|
- Open the Threads window (Alt-Shift-7).
- Right-click the
thread and choose Suspend or Resume.
|
|
Step into JDK classes.
|
- Open the Sources
window (Alt-Shift-8).
- 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.
|
- Open the Call Stack
window (Alt-Shift-3).
- Right-click the
call that you want to remain at the top of the call stack.
- 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
|
|