Debugging with Eclipse
A debugger is a tool used to do precisely what it suggests: eliminate bugs from a program. Generally, debuggers are computerized code tracers which allow programmers to step through code. With each step, debuggers may display updated variable changes or make changes to variables on the fly. It turns out that Eclipse has a debugger, which does all of this and more.
Let’s take a look at the following piece of buggy code, intended to find the largest element in an array a:
|
To your dismay, the code prints out 1 as the answer. The error lies in line 9, where the > is supposed to be replaced with <. Let’s see how we can use the Eclipse debugger to help us spot this problem.
Starting the Debugger
The first step to debugging this code is setting a breakpoint. A breakpoint is usually needed to set the starting point for the debugger. It is also used to jump over segments of code; we’ll cover that in a later issue.
Set a breakpoint on line 2; do this by either double-clicking in the gutter, the gray margin to the left of the coding area, or using the shortcut Ctrl-Shift-B while the cursor is on line 2.
Start the debugger by either clicking on the Debug button on the Eclipse toolbar, or click F11.
If using the debugger for the first time, Eclipse should ask you whether it should preserve the perspective changes for the Debug view. Click yes and select the checkbox to remember this choice.
Debugging the Code
If you followed the steps above properly, you should now see a highlighted line in your code with a blue arrow in the gutter, where you placed your first breakpoint; the array declaration should be highlighted. The blue arrow lets you know where the program is in execution; it hasn’t executed the highlighted line yet, but will do so as soon as you continue running the code. The most commonly-used commands to step through code are located in the Debug window, where you should see a play button, a stop button, and various other buttons.
| Click on the “step over” button (F6). You should observe that a new entry appears in the Variables window, a. Click on the variable a in the Variables window. You should notice that the contents of the array appear immediately below the window. Click on the “step over” button again. You should observe that max appears in the Variables window, and is appropriately set to 1. Click “step over” two more times, until the blue arrow is on line 3, above. What do you think will happen if you step over once more? Since the conditional evaluates to false, it should be that the blue arrow jumps back to line 2. Click “step over.” Sure enough, the blue arrow does indeed move back to line 2. What happened to val? It disappeared, since the variable went out of scope. Click “step over” again. You should notice that i in the Variables window is now highlighted yellow. This is because the value of i changed during execution. Click “step over” again. Line 9 should now be highlighted. What do you think will happen when you click “step over” again? As of now max is 1 and val is 2. Click “step over” again. We see that max isn’t updated, even though the value encountered was greater. We saw that the problem here occurred on line 9. We now realize that the > should be a <. |
This was a pretty simple application of Eclipse’s debugger. How do you get better at debugging? Practice, practice, practice.


