Debugging your programs

In this section, you will debug a Java program.

  1. In the Package Explorer view in the Java perspective, double-click junit.samples.VectorTest.java to open it in an editor.
  2. Place your cursor on the vertical ruler along the left edge of the editor area on the following line in the setUp() method:

    fFull.addElement (new Integer(1));

    and double-click on the ruler to set a breakpoint.

    VectorTest.java editor showing added breakpoint

    The breakpoint icon indicates the status of the breakpoint.  The plain blue breakpoint icon indicates that the breakpoint has been set, but not yet installed.  

    Note: Once the class is loaded by the Java VM, the breakpoint will be installed and a checkmark overlay will be displayed on the breakpoint icon.

  3. In the Package Explorer view, select the junit.samples package and select the Run menu. Within the Run menu, select Debug As, and then Java Application.  When you run a program from a package, you will be prompted to choose a type from all classes in the package that define a main method. 
  4. Select the VectorTest - junit.samples - /JUnit item in the dialog, then click OK.

    Type selection dialog

  5. The program will run until the breakpoint is reached.  When the breakpoint is hit, the Debug perspective opens, and execution is suspended. Notice that the process is still active (not terminated) in the Debug view. Other threads might still be running.

    Debug perspective

    Note: The breakpoint now has a checkmark overlay since the class VectorTest was loaded in the Java VM.
  6. In the editor in the Debug perspective, select the entire line where the breakpoint is set, and from its context menu, select Inspect.

    VectorTest.java editor with selected breakpoint line showing context menu

  7. The expression is evaluated in the context of the current stack frame, and the results are displayed in the Expressions view.
  8. Expressions that you evaluate while debugging a program will be listed in this view. To delete an expression after working with it, select the expression and choose Remove from its context menu.
  9. The Variables view (available on a tab along with the Expressions view) displays the values of the variables in the selected stack frame. Expand the fFull tree in the Variables view until you can see elementCount.

    Variables view

  10. The variables (e.g., elementCount) in the Variables view will change when you step through VectorTest in the Debug view.  To step through the code, click the Step Over button.  Execution will continue at the next line in the same method (or, if you are at the end of a method, it will continue in the method from which the current method was called).
  11. Try some other step buttons (Step Into, Step Return) to step through the code.  Note the differences in stepping techniques.
  12. You can end a debugging session by allowing the program to run to completion or by terminating it.

Related concepts
Breakpoints
Remote debugging
Local debugging

Related tasks
Adding breakpoints
Resuming the execution of suspended threads
Running and debugging
Suspending threads

Related reference
Debug preferences
Debug view
Run and debug actions
Breakpoints view
Console view
Display view
Expressions view
Variables view

Copyright (c) 2000, 2003 IBM Corporation and others. All Rights Reserved.