Home > Articles > Programming > Java

  • Print
  • + Share This
This chapter is from the book

Stepping Through Code

Once execution of your program is paused, you have several ways of resuming execution of the code. You can step through code line by line (Step In) or in greater increments. See Table 5-4 for the commands available for stepping or continuing execution and the following subtopics for a task-based look at the commands.

Table 5-4. Debugger Step Commands

Step Command

Description

Step Into (F7)

Executes the current line. If the line is a call to a method or constructor, and there is source available for the called code, the program counter moves to the declaration of the method or constructor. Otherwise, the program counter moves to the next line in the file.

Step Over (F8)

Executes the current line and moves the program counter to the next line in the file. If the executed line is a call to a method or constructor, the code in the method or constructor is also executed.

Step Out Of (Alt-Shift-F7)

Executes the rest of the code in the current method or constructor and moves the program counter to the line after the caller of the method or constructor. This command is useful if you have stepped into a method that you do not need to analyze.

Run to Cursor (F4)

Executes all of the lines in the program between the current line and the insertion point in the Source Editor.

Pause

Stops all threads in the current session.

Continue (Ctrl-F5)

Resumes execution of the program until the next breakpoint.

Executing Code Line by Line

You can have the debugger step a line at a time by choosing Run | Step Into (F7). If you use the Step Into command on a method call, the debugger enters the method and pauses at the first line, unless the method is part of a library that you have not specified for use in the debugger. See Stepping into the JDK and Other Libraries later in this chapter for details on making sources available for use in the debugger.

Executing a Method without Stepping into It

You can execute a method without having the debugger pause within the method by choosing Run | Step Over (F8). After you use the Step Over command, the debugger pauses again at the line after the method call.

Resuming Execution Through the End of a Method

If you have stepped into a method that you do not need to continue analyzing, you can have the debugger complete execution of the method and then pause again at the line after the method call.

To complete execution of a method in this way, choose Run | Step Out Of (Alt-Shift-F7).

Continuing to the Next Breakpoint

If you do not need to observe every line of code while you are debugging, you can continue execution until the next breakpoint or until execution is otherwise suspended.

To continue execution of a program that has been suspended at a breakpoint, choose Run | Continue or press Ctrl-F5.

Continuing to the Cursor Position

When execution is suspended, you can continue to a specific line without setting a breakpoint by placing the cursor in that line and choosing Run | Run to Cursor (F4).

Stepping into the JDK and Other Libraries

When you are debugging, you can step into the code for the JDK and any other libraries if you have the source code that is associated with them registered in the IDE's Library Manager. See Making External Sources and Javadoc Available in the IDE in Chapter 3 for information on associating source code with a library.

By default, the IDE does not step into JDK sources when you are debugging. If you use the Step In command on a JDK method call, the IDE executes the method and returns the program counter to the line after the method call (as though you used the Step Over command).

To enable stepping into JDK sources for a debugged application:

  1. Start the debugger for the application.
  2. Open the Sources window (shown in Figure 5-4) by choosing Window | Debugging | Sources or by pressing Alt-Shift-8.
    05fig04.gif

    Figure 5-4 Sources window

  3. Select the Use for Debugging checkbox for the JDK.

Limiting the Classes That You Can Step into for a Library

If you are using a library for debugging, you can set a filter to exclude some of the sources from being used.

To exclude classes from being used in the debugger:

  1. Start the debugger for the application.
  2. Open the Sources window by choosing Window | Debugging | Sources or by pressing Alt-Shift-8.
  3. Right-click the line for the library that you want to create an exclusion filter for and choose Add Class Exclusion Filter.
  4. Type a filter in the Add Class Exclusion Filter dialog box.

The filter can be

  • A fully qualified class name.
  • A package name or class name with an asterisk (*) at the end to create a wildcard. For example, you could type the following to exclude all classes in the javax.swing package: javax.swing.*
  • An expression with a wildcard at the beginning. For example, to exclude all classes that have Test at the end of their names, you could use: *Test

You can create multiple class exclusion filters.

To disable the filter, deselect the Use in Debugging checkbox next to the filter in the Sources window.

To delete a class exclusion filter, right-click the filter and choose Delete.

  • + Share This
  • 🔖 Save To Your Account