Using
the Eclipse Debugger for Beginning Programmers
Andy Meneely
CSC 116 - Introduction to
Java Programming
Department of Computer Science
North Carolina State University
Back
to Software Engineering Tutorials
0.0
Contents
Target Audience: this tutorial is geared toward the
beginning Java programmer who has some experience with Java programming
in Eclipse. If you are not familiar with Eclipse, our getting started tutorial is a great
place to learn.
1.0 Introducing Eclipse
A note about audience. We will be assuming two different
users of this tutorial:
- Home users: users working on their own machine, who can
devote permanent space to their Eclipse work.
- Lab users: users working in public laboratories who
cannot devote permanent space to their Eclipse work. Lab users may need
some special information specific to their lab, such as the proper
location of their workspace. We may defer to your lab instructor
to get this specific information.
2.0 Installing and Starting Eclipse
Lab Users: your lab should already have Eclipse 3.4 (or
similar version) installed. Start Eclipse the way your lab instructor
tells you to. Skip to the Starting Eclipse part.
Home Users: follow these instructions.
- Go to http://eclipse.org and
click on
Download Eclipse.
- Find the
Eclipse for Java Developers link on the
page. Click on the link for your operating system along the right-hand
side.

Figure 1: Downloading the Eclipse for Java Developers
- Select a mirror and download the file. Make sure that you are
downloading a zip file.
- Once the zip file is downloaded, unzip the file to a location
on your computer. I usually choose something like
c:\eclipse-3.4.
Remember this location - we will refer to this location as <YOUR
INSTALL LOCATION>.
- Start Eclipse by executing
<YOUR INSTALL
LOCATION>/eclipse/Eclipse.exe
Starting Eclipse
When you first start Eclipse, you will be asked to select your
workspace. Like this:
Choosing a Workspace
The Eclipse workspace is a permanent location for all of
your work.
- Lab users: choose the location of your workspace
according to what your lab instructor tells you. Please note that your
work may not be backed up on laboratory machines, so be sure to copy
your workspace to a safe place before logging out.
- Home users: choose any location of your workspace on
your hard drive. I usually choose something like
c:\workspace.
The first time you start with a fresh workspace, you will be
shown a Welcome view. Close the Welcome view
according to the following figure
Figure 2: Closing the Welcome Screen
In most Eclipse versions, your interface looks like Figure 3.
This is called the Java Perspective (which will have this icon
in the upper-right:
). Feel free to mess
around with the windows (called "Views"). If you want to reset the
perspective to how it originally looked, go to Window >
Reset Perspective. If you are not in the Java perspective, use Window
> Open Perspective > Other... > Java (NOT Java Browsing!)
Figure 3: The Java Perspective
3.0 Stepping through the main
method
Now that we have our Eclipse workspace set up, let's set up our project.
Let's start by creating a project called NumberPrinter,
then step through it using the Eclipse Debugger.
- Right click in the
Package Explorer (
) and select New >
Java Project. A window will pop up where you can change setting related
to your project as shown in Figure 4. Place NumberPrinter
as the name of your project. The other default settings are okay. Hit
Finish.

Figure 4: Creating a new Java Project
- Right click on the src (
) folder and
go to New > Class

Figure 5: Creating a new Java Class
- For the name of your class, type
Fibonacci. The
other default settings are okay, so you can hit Finish.

Figure 6: Creating a new Java Class Dialog
- Copy-and-pase our example
Fibonacci.java code over your Fibonacci code
so that the class compiles.

Figure 7: Adding
Fibonacci.java
- Now let's add a breakpoint to our
main
method. A breakpoint is a location in your source code that will halt
execution so that you can view the information about a program's state.
To create a breakpoint, right-click along the left side of the Java
editor on the line of code you wish to break on, (Also, double-clicking
on the left side of the editor will create a breakpoint.) The red arrow
in Figure 8 shows where to click. Create a breakpoint on the line where
n is initialized.

Figure 8: Adding a breakpoint
- A blue dot (
) will appear next to
the line of code where the execution will halt.
Now we need to run our program through the debugger. This works
similarly to running a program: right click on the Fibonacci.java
file in the Package Explorer and select Debug As > Java Application

Figure 9: Running in the debugger
- You may get a message about switching perspectives - this is
okay. Feel free to check the "Remember my decision" so that you don't
see this dialog again.

Figure 10: Confirming Perspective Switch
- Below is a screenshot of the debug perspective. Let's take a
moment to look at all of the views

Figure 11: Confirming Perspective Switch
- Debug: shows the live stack trace of methods. We are
currently only in the main method of the Fibonacci program. If other
programs were currently running, we would see them here. Also has some
stepping buttons that we will cover shortly.
- Variables: shows the values of all the variables that
have been declared. Since our breakpoints stops before
n
was declared, we don't see it yet.
- Breakpoints: this view shows the location of all our
breakpoints in the code (we can have many different breakpoints).
- Fibonacci.java: we can view where are in our code at
the time of the breakpoint
- In the Debug view, press the Step Over (
) button to go to the next line. Notice that
the new variable n has been declared and has a value of
zero. As you can imagine, knowing the values of your variables
eliminates the need for using println statements in your
code.

Figure 12: Stepping Over
- Notice now that we are in the Do loop and suspended on the
next line. Step Over will go to the next line in your method and then
halt (unless it has some reason to halt before going to the next line).
Add a breakpoint on the printArray call.

Figure 13: A new breakpoint at the end of the method
- Now let's let the program run until it comes to another
breakpoint. Press the Resume (
) button in
the Debug view to release the current execution.
Notice now that our program has not finished, but is not doing
anything. For example, That's because the program is prompting us for
an integer. We can see that our program (here called a "Thread") is not
suspended but running in the Debug view. Now it says (Running), whereas
in Figure 11 it said (Suspended (breakpoint at line 6 in Fibonacci))

Figure 14: The thread is in a running state
- In the Console view, type in the number 13 and hit enter.

Figure 15: Resuming execution after the prompt
- Now we are breaking before the method calls to
getFiboArray
and printArray.

Figure 16: Breaking before the method calls
4.0 Stepping through multiple methods
Now that we've stepped through most of the main
method, let's step into some other methods:
- Make sure you have completed the previous section before
starting this section. You should be suspended at the line with the
method calls to
getFiboArray and printArray.
Now the current line we are halted on does a lot. First, the method getFiboArray
gets executed, then printArray gets executed. If we hit
the Step Over button, now, we would skip over the methods. To go into a
method, we need the Step In (
) button.
Press the Step In button now.

Figure 17: Stepping into
getFiboArray
- Note that the Variables view shows new variables. These are
the variables that are currently in scope. The variables
declared in our
main method are not in scope right now, so
we no longer see them.

Figure 18: The scope of
getFiboArray
- We can now add breakpoints or step through this method as we
wish. Let's use another way to step through a program without
breakpoints or going line-by-line: "Run to Line. Right-click on the
line that has:
f[1] = 1; and choose Run to Line. This
works just like a breakpoint - execution goes until the line we
specify.

Figure 19: Run to line.
- Now let's suppose that we want to finish our current method
and go back to the caller. For that, we use the Step Return (
) button in the Debug view. Hit the Step
Return button to return to the calling line in the main method.

Figure 20: Step Return.
- Pressing the Step In button will now bring us into the
printArray
method.

Figure 20: Step Return.
- Press Resume to finish the program.
5.0 Special Breakpoints and Watching
Expressions
Special Breakpoints
Specific line numbers aren't the only situations where we can
specify breakpoints, we can tell our debugger to halt whenever an
exception occurs, for example.
- Make sure your program is not currently running. In the Debug
Perspective, click on the Breakpoints view and then click on Remove All
Breakpoints (
).
- Now let's set up a breakpoint for when an
ArrayIndexOutOfBounds
exception occurs. In the breakpoints view, click on the Add Java
Exception Breakpoint button (
).
A dialog box for select the exception type comes up:

Figure 21: Choosing an exception to break on.
- The dialog is set to break on any exception by
default. Start typing
ArrayIndexOutOfBoundsException to
filter the results. Hit Ok when you've chosen java.lang.ArrayIndexOutOfBoundsException

Figure 22: Choosing an exception to break on.
- Now let's inject an exception into our code. In the method
getFiboArray,
change the < sign to a <=. This means our loop will go too far
and access an element of the fibo array outside of its bounds.

Figure 24: Injecting a bug.
- Run Fibonacci under the debugger again (use the Run > Debug
history > Fibonacci menu). Input the number 13 at the prompt again. If
you cleared all of your breakpoints, the debugger will halt when the
exception was thrown. You can see that an exception occurred in the
Debug view where it says (Suspended
(exceptionArrayIndexOutOfBoundsException))

Figure 25: Injecting a bug.
- This is most useful for seeing the values of all your
variables at the time of an exception. Press the Resume button to let
the program finish.
Watching Expressions
Not only can we keep track of variables in a debugger, we can
also keep track of particular expressions throughout the program using
Watch Expression.
- Fix the injected bug from the previous section.
- Add a breakpoint to the beginning of the getFiboArray method
and start the debugger. Enter 13 at the prompt again to get to our
breakpoint in the getFiboArray method.

Figure 26: Debugging the getFiboArray function
- Notice that, a few lines down, there is the expression:
f[i
- 1] + f[i - 2]. Highlight that expression and right click. Go to
Watch.

Figure 27: Watching an expression
- Notice that an Expression view popped up. We have our
expression that is currently being watched.
Note that our expression has no value yet. Expanding the tree shows
that neither f nor i can be resolved - yet.
That's because we are halted before we have declared the array
f or the variable i.

Figure 28: Watching an expression
- Use Step Over to step through the method. When you get into
the loop, notice that our expression is updated. This can be a very
helpful tool to keep track of complex expressions.

Figure 29: Expression is updated as we step through the program
- If you only want to peek at a particular expression, but not
track it, use the Display option in the right-click menu.

Figure 30: Selecting Display

Figure 31: Displaying an expression
6.0 Resources
There are a multitude of other resources related to Eclipse out
there. Here are just a few:
Back to Software Engineering Tutorials
© 2008 North
Carolina State University, Laurie Williams and Andy Meneely
Last Updated: November 11, 2008
Questions? Comments? Contact our mailing list:
se_tutorials@lists.ncsu.edu