CSC 207 Grinnell College Spring, 2012
 
Algorithms and Object-Oriented Design
 

Basic Debugging with Eclipse

Summary

This reading describes some basic capabilities for debugging Java programs with Eclipse.

Introduction

Eclipse provides extensive capabilities for program debugging. Some commonly used capabilities involve

The following notes describe each of these capabilities.

Running Example

The following discussion uses these files as running examples. All files are from the reading on input in Java.

Setting Breakpoints

A breakpoint is a specified line within a program, at which point execution will stop until further instructions are given. After loading the Java class into Eclipse, you can choose one or more places at which you want execution to pause. To set (or unset) a breakpoint:

After you set a breakpoint, a blue dot will appear in the vertical blue bar.

For this running example, set a breakpoint for the second line of printing headings, in which the text "First Last ..." is printed.

Entering the Debugger

After setting breakpoints, run the debugger:

In the example, the first System.out.println statement will have executed, and the output will be shown in the Console window at the bottom. Also, the second println statement will be colored green.

Once execution has paused, many options are available, some of which are described below.

Examining Values in Variables

Within the debugger, another tab, next to "Breakpoints", is labeled "Variables". Scrolling through this window will display all currently declared variables and their current values.

In the example, you will see that variable testFile contains the full path name of the data file, and all test scores, maximums, and minimums have 0 values.

Stepping Through Program Execution

Once program execution has paused, you have several options for continuing program execution. You can choose these options either using a function key on the keyboard or by clicking a button to the right of the Debug tab on the upper-left of Eclipse.

Keyboard
Button
Icon near Debug tab Meaning
F5 Step Into Execute a method call, and follow the step-by-step tracing within the new method. Execution moves into the called method, and execution stops at the first line of this new method.
F6 Step Over Execute a method call, allow the called method to execute fully, and stop at the next line of the current method.
F7 Step Return Finish executing this method and return. Execution stops in the line that called this method.

As you click one icon or keyboard function key, Eclipse performs the desired activity, and all windows are updated. The icons become enabled or disabled, according to whether the next line calls a method, returns from a method, or allows a single step forward.

Watching Expressions

Within the class listing, highlight an expression (such as line.substring (31).trim()), right click, and select the "Watch" option. A new "Expressions" tab will appear next to "Variables" and "Breakpoints". This tab will show you the current value of this expression as program execution continues.

Right clicking on an expression in the "Expressions" window allows you to edit the expression. Effectively this means you can follow changes in any expression involving active variables during program execution.

Further Example

Edit the testFile, so that the file name is test1.data rather than test.data. This file contains the same data as before, except that the third person in the file "Francis Homer" has four test scores rather than three. Executing the program step by step allows you to follow the values of various variables, and you can identify just what goes wrong when this line is read from the file. In particular, when the program fails, an exception is thrown and execution jumps to an exception handler. Click on the TestScanner1.java main class in the "Debug" window, so you can examine the variables to determine the root cause of the problem. (Check the intString variable, for example.)

Similarly, you can edit the testFile in TestScanner1.java, so that the program wil read test2.data. In this case, the second person has only two test scores rather than three, and you can follow what goes wrong when the data file is processed.

Returning to Java Mode

Once you have completed your work with the debugger, you can return to the normal Java mode:


This document is available on the World Wide Web as

http://www.walker.cs.grinnell.edu/courses/207.sp12/readings/reading-input.shtml

created 9 February 2012 by Henry M. Walker
last revised 9 February 2012
Valid HTML 4.01! Valid CSS!
For more information, please contact Henry M. Walker at walker@cs.grinnell.edu.


Copyright © 2011-2012 Henry M. Walker.
CC-BY-NC-SA
This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 United States License .