Debug an Application

The following topics describe the basic sequence of activities completed by a developer when debugging an application for the Java Card 3 platform:

Prepare to run the debugger Use the IDE to:
Run the debugger
Monitor your code
  • Step through your code:
    Debug > Step option
  • View debugging information.
  • View application threads.
  • Fix any errors and continue debugging.
Finish the debugger session
  • Stop the debugger:
    Debug > Finish Debugger Session

Set Breakpoints in Code

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 such as examining the values of variables and single-stepping through your program.

The IDE enables you to set several types of breakpoints using the New Breakpoint dialog. Breakpoints can be set using the New Breakpoint dialog for the following types of source elements:

Class
You can break when the class is loaded into the virtual machine, unloaded from the virtual machine, or both.
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 error or not.
Field
You can stop execution of your program whenever a field in a specific class is accessed (for example, the method was called with the variable as an argument), modified or both.
Method
Program execution stops every time the method is entered, exited or both. Thread. You can break program execution whenever a thread starts, stops, or both.

Set Watches in Code

A watch enables you to track the changes in the value of a variable or expression during program execution. The Watches window opens at the bottom of the editor and lists all of the watches you have defined for your project. You can create a watch directly from the Source Editor or with the Debug > New Watch menu selection.

When you create a watch, the value of the variable or expression is immediately evaluated and displayed in the Watches window. The value of the watch is based on the current context. When you change the current context, the Watches window is updated to show the value of the watch for that context. When a debugging session is running, you can use code completion in the New Watch dialog box.

To create a watch:

  1. Select the variable or expression in the Source Editor.
  2. Either right-click on the selection and choose New Watch or select Debug > New Watch in the IDE menu bar.
  3. The New Watch dialog box opens with the variable or expression entered in the text field.
  4. Click OK.

The Watches window opens at the bottom of the IDE with the new watch selected.

Start a Debugging Session

All debugging commands in the Debug menu are run on the main project. Regardless of the file or project selected in the Projects window or Source Editor, the Debug menu commands begin a debugging session in the main class of the main project.

To debug an individual project, right-click the project in the Projects window and choose Debug. The IDE runs the project in the debugger until execution stops or a breakpoint is reached.

To debug an individual file, select a runnable file in the Projects window and choose Debug > Debug File. The IDE runs the file in the debugger until execution stops or a breakpoint is reached.

Stepping Through Code

When execution of your program is halted, you can step through your lines of code using the following commands on the Debug menu or toolbar:

Step Over
Executes one source line. If the source line contains a call, executes the entire routine without stepping through the individual instructions.
Step Over Expression
Executes one method call in an expression. If an expression has multiple method calls, you can use Step Over Expression to step through an expression and view the value of each method call in the expression in the Local Variables window. Each time you use the Step Over Expression command, the debugger advances to the next method call in the expression and the completed method call is underlined. Step Over Expression behaves like Step Over when there are no additional method calls.
Step Into
Executes one method call in a source line. If the line has more than one method call you can choose which method call to step into by using the arrow keys or mouse in the source editor to select the method call. The selected method call to step into is indicated by a box around the method call in the source editor. The most likely method call in the line is selected by default.
Step Into Next Method
Executes one source line. If the source line contains a call, the IDE stops just before executing the first statement of the routine. You can also start a debugging session with the Step Into command. Program execution stops on the first line after the main routine before any changes have been made to the state of the program.
Step Out
Executes one source line. If the source line is part of a routine, executes the remaining lines of the routine and returns control to the caller of the routine. The completed method call is highlighted in the Source Editor.