Debugging a Java Program with Eclipse
Stepping through code
In the title bar of the Debug view is a toolbar that lets you control the program's execution. The first few tool buttons, which resemble the familiar controls of electronic devices such as CD players, allow you to resume, suspend, or terminate the program. Several buttons incorporate arrows in their design; these allow you to step through a program a line at a time. Holding the mouse over each button in turn will cause tool tips to appear, identifying them as Step With Filters, Step Into, Step Over, Step Return, and so on.
For example, click the second step button, Step Into. Doing so executes the line of code that is currently highlighted in the editor area below the Debug view: the call to the say() method. Step Into, as the name suggests, takes you into the method that is called. After clicking Step Into, the highlighted line is the first executable line in say()—the for statement.
Evaluating variables and expressions
To the right of the Debug view is a tabbed notebook containing views that let you examine and modify variables and breakpoints. Select the Variables tab (if it isn't already selected). This view shows the variables in the current scope and their values; before entering the for loop, this view includes only the say() method's msg parameter and its value, "Hello, world!" Click either Step Over or Step Into to enter the for loop. (Both have the same effect here, because you don't call any methods in this line of code.) The Variables view will display the loop index i and its current value, 0.
Sometimes a program has many variables, but you're interested in only one or a few. To watch select variables or expressions, you can add them to the watch list in the Expression view. To do this, select a variable—i, for instance—by double-clicking on it in the editor, and then right-clicking on the selection and choosing Watch from the context menu. The variable (and its value, if it's in scope) will appear in the Expressions view.
One significant advantage of watching variables in the Variables and Expressions views over using print statements for debugging is that you can inspect objects and their fields in detail and change their values—even normally immutable strings. Return to the Variables view and expand the msg variable to show its attributes. One of these is a char array, value, which can be expanded to reveal the individual characters in the msg String. For example, double-click on the character H, and you will be prompted to enter a new value, such as J.
The Display view is in the same tabbed notebook. It allows you to enter any variables that are in scope, or arbitrary expressions including these variables. Select Display view and enter the following, for example:
To immediately evaluate this expression, you must first select it and then click the second Display view tool button (Display Result of Evaluating Selected Text), which displays the results in the Display view. It's usually better to click the first tool button (Inspect Result of Evaluating Selected Text), because it adds the expression to the Expressions view. Either way, the value displayed is not automatically updated as the variables in the expression change; but in the Expressions view, you have the option of converting the expression into a watch expression, which is updated as you step through the code. To do this, change to the Expressions view. Notice that the Inspect icon (a magnifying glass) appears next to the expression. Click on the expression and select Convert to Watch Expression from the context menu. The icon next to the expression will change to the Watch icon.
Let's go back to stepping through the code. You previously left the cursor at the call to System.out.println(). If you want to see the code for System.out.println(), you can click Step Into; otherwise, click Step Over to execute the System.out.println() method and start the next iteration of the for loop.
Below the editor area is another tabbed notebook, which includes a Console view. Program output appears here; if you made the earlier change to the variable msg, the line "Jello, world!" will appear. You can either continue to click Step Over until the loop terminates or, if you find this process tedious, click Step Return to immediately finish executing the say() method and return to the main() method. Or, just click the Resume button to let the program run to the end.
One of Eclipse's most useful features is its integrated debugger. The ability to execute code interactively—setting breakpoints, executing code line by line, and inspecting the value of variables and expressions—is powerful tool for investigating and fixing problems with the code.
About the Author
David Gallardo is an independent software consultant specializing in software internationalization, Java Web applications, and database development. His recent experience includes leading database and internationalization development at a business-to-business e-commerce company, TradeAccess, Inc. He was also a senior engineer in the international product development group at Lotus Development Corporation, where he contributed to the development of a cross-platform library providing Unicode and international language support for Lotus products including Notes and 1-2-3. He is the co-author of Eclipse in Action published by Manning Publications Co. and author of Java Oracle Database Development. He lives in El Paso, Texas.
Page 2 of 2