the Eclipse Debugger for Beginning Programmers
Andy Meneely and Sarah Heckman
CSC 116 - Introduction to
Department of Computer Science
North Carolina State University
to Software Engineering Tutorials
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
When you first learned how to write Java programs, you probably
started by writing in a text file, compiling using the javac command, then running your program from the command-line using the java command. This form of
programming is what we'll refer to as command-line programming, and follows the practice of edit-compile-execute.
While command-line programming has many advantages, it can be quite
tedious. After all, your text editor doesn't usually provide much
outside of... well... editing. Professional developers, generally
speaking, tend to gravitate toward what is called an integrated
development environment (IDE) for their programming. The idea of an IDE
is to integrate many different tools into a single program (e.g. code
editing, compiling, running, file management, code navigation,
documenting, etc.) Programming with an IDE can provide some of the
following advantages over command-line programming:
- Compiling is built-in. Whenever you hit "Save", your
code is compiled for you.
- A controlled execution environment means that you can
control how exactly your program will be executed without changing what
your current system is configured for (e.g. running your program using
different Java virtual machines)
- A Graphical User Interface (GUI) provides immediate
feedback on compile errors, warnings, and other problems with your
- IDEs are highly customizable. From formatting and
coloring preferences to programming in various languages and
technologies, IDEs are extremely extensible.
- Code editing and navigation is much easier
because the IDE "understands" the language you're working with (you'll
understand more of what this means with a few examples).
- IDEs support large projects by providing many other
tools not related directly to programming, but with software
development in general.
In this tutorial we'll be covering Eclipse, a professional-grade,
open-source development platform for Java (and many other languages!).
We'll focus on some of the features found in Eclipse that are not found
in command-line programming.
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
- Find the
Eclipse for Java Developers link on the
page. Click on the link for your operating system along the right-hand
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
Remember this location - we will refer to this location as
- Start Eclipse by executing
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
- 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
The first time you start with a fresh workspace, you will be
Welcome view. Close the
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
Reset Perspective. If you are not in the Java perspective, use
> Open Perspective > Other... > Java (NOT Java Browsing!)
Figure 3: The Java Perspective
3.0 Stepping through the
Now that we have our Eclipse workspace set up, let's set up our project.
Let's start by creating a project called
then step through it using the Eclipse Debugger.
- Right click in the
Package Explorer () and select
Java Project. A window will pop up where you can change setting related
to your project as shown in Figure 4. Place
as the name of your project. The other default settings are okay. Hit
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
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
- Now let's add a breakpoint to our
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
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
In the Debug view, press the Step Over () button to go to the next line. Notice that
the new variable
- 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
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
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
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).
Figure 12: Stepping Over
Add a breakpoint on the
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.
Figure 13: A new breakpoint at the end of the method
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))
In the Console view, type in the number 13 and hit enter.
Figure 14: The thread is in a running state
Now we are breaking before the method calls to
Figure 15: Resuming execution after the prompt
Figure 16: Breaking before the method calls
4.0 Stepping through multiple methods
Now that we've stepped through most of the
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
Now the current line we are halted on does a lot. First, the method
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
- 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
- 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; and choose Run to Line. This
works just like a breakpoint - execution goes until the line we
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
Figure 20: Step Return.
- Press Resume to finish the program.
5.0 Special Breakpoints and Watching
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
- Now let's set up a breakpoint for when an
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
filter the results. Hit Ok when you've chosen
Figure 22: Choosing an exception to break on.
- Now let's inject an exception into our code. In the method
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
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.
Not only can we keep track of variables in a debugger, we can
also keep track of particular expressions throughout the program using
- 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:
- 1] + f[i - 2]. Highlight that expression and right click. Go to
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
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
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: Tuesday, May 25, 2010 1:55 PM
Questions? Comments? Contact our mailing list: