Eclipse IDE Debugger Tutorial
Adapted from a tutorial at the University of Wisconsin
The Eclipse Debugger is a tool that you can use
to analyze your program while it's running.
A debugger is a useful tool, but it is no substitute for careful
thought. Stepping through code in a debugger takes time. You should
always have a debugging plan before you start up a debugger.
Sample debugging plan:
- Identify the problem (bug). State it as precisely as you can.
- Determine what the correct behavior (output) should be.
- Design a test that fails due to the bug. (I.e., a set of actions that can reliably reproduce the failure/bug. For example, "if I enter a number less than zero, I should get 'happy', but instead I'm getting 'sad'.")
- Identify one or more sections of code (or variables) that control that
aspect of your program. (Continuing our example, look for code that prints "happy" or "sad". That's a possible starting point.)
- Set breakpoints at or before the bug occurs and use a debugger
to trace each section of code and view the variables
and results until a variable's value or output is not as expected.
- Continue narrowing your search until you find the line(s) that do not
produce the expected result.
- Edit those lines and re-run the test from step 3.
- Repeat these steps until all of your tests pass and you have no bugs.
If you find yourself stepping through a program with no idea of what you
are trying to find out or with no idea of the correct execution, you
are wasting your time. Spend some time away from the debugger thinking
about the problem and what might be causing it.
This tutorial guides you through the basic functions of the Eclipse debugger.
We will use as an example a small project that you can experiment with as you follow
the steps of the tutorial.
Set up the tutorial project:
- Create a new project named DebugTutorial in Eclipse.
- Download the following files and add them to your project (or use this zip file and decompress it).
- Refresh the project listing to see the newly added files.
- Add javabook2.jar and tutorial.jar as
external .jar files to your project.
Refer to the Eclipse Tutorial: Step 4
for instructions on adding external jar files if necessary.
- Run the project before you begin the tutorial. (Aquarium is the main class.) The project is a very
simple simulation of an Aquarium. When you are prompted, enter the following values:
- Number of fish:
3
- Number of piranha:
2
- Number of food centers:
5
- Choose Continuous Mode to see the simulation.
- Quit the simulation after you have witnessed each of these bugs:
- The incorrect number of fish are created.
- The food centers are not showing the correct food point values.
The following pages take you through the basic steps of using a debugger tool to find and fix these bugs.
Select a topic to review, or click Start below to begin the tutorial.
- Switch to the Debug Perspective.
- Set one or more Breakpoint(s).
- Run the Debugger.
- Step Over a Method Call.
- Step Into a Method.
- Inspecting Variables.
- Step Out Of a Call to a Method.
- Review the Method Call Stack.
- Finish the Tutorial.