Using the Eclipse Debugger for Beginning Programmers

Andy Meneely and Sarah Heckman
CSC 116 - Introduction to Java Programming
Department of Computer Science
North Carolina State University

Back to Software Engineering Tutorials

0.0 Contents
1.0 Introducing the Eclipse Debugger
2.0 Installing and Starting Eclipse
3.0 Stepping through the main method
4.0 Stepping through multiple methods
5.0 Special Breakpoints and Watching Expressions
6.0 Resources

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:

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:

Top | Contents

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.

  1. Go to and click on Download Eclipse.
  2. Find the Eclipse for Java Developers link on the page. Click on the link for your operating system along the right-hand side.
    Eclipse for Java Developers
    Figure 1: Downloading the Eclipse for Java Developers
  3. Select a mirror and download the file. Make sure that you are downloading a zip file.
  4. 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>.
  5. 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:

Eclipse workspace
Choosing a Workspace

The Eclipse workspace is a permanent location for all of your work.

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

Eclipse welcome screen
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: Java perspective). 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!)

Eclipse User Interface
Figure 3: The Java Perspective
Top | Contents

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.

  1. 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
  2. Right click on the src () folder and go to New > Class

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

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

  7. Figure 7: Adding
  8. 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.

  9. Figure 8: Adding a breakpoint
  10. 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

  11. Figure 9: Running in the debugger
  12. 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.

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

  15. Figure 11: Confirming Perspective Switch
  16. 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.

  17. Figure 12: Stepping Over
  18. 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.

  19. Figure 13: A new breakpoint at the end of the method
  20. 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))

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

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

  25. Figure 16: Breaking before the method calls
Top | Contents

4.0 Stepping through multiple methods

Now that we've stepped through most of the main method, let's step into some other methods:

  1. 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.

  2. Figure 17: Stepping into getFiboArray
  3. 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.

  4. Figure 18: The scope of getFiboArray
  5. 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.

  6. Figure 19: Run to line.
  7. 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.

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

  10. Figure 20: Step Return.
  11. Press Resume to finish the program.
Top | Contents

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.

  1. Make sure your program is not currently running. In the Debug Perspective, click on the Breakpoints view and then click on Remove All Breakpoints ().
  2. 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:

  3. Figure 21: Choosing an exception to break on.
  4. 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.
  5. 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.

  6. Figure 24: Injecting a bug.
  7. 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))

  8. Figure 25: Injecting a bug.
  9. 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.

  1. Fix the injected bug from the previous section.
  2. 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.

  3. Figure 26: Debugging the getFiboArray function
  4. 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.

  5. Figure 27: Watching an expression
  6. 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.

  7. Figure 28: Watching an expression
  8. 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.

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

  11. 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: Tuesday, May 25, 2010 1:55 PM

Questions? Comments? Contact our mailing list: