Debugging
Brandon Krakowsky
Debugging in Eclipse
Property of Penn Engineering |
What is Debugging?
• Debugging allows you to run a program interactively while watching the source code and the
variables during the execution
• A breakpoint in the source code specifies where the execution of the program should stop
during debugging
• Once a program is stopped, you can investigate variables (and even change their content)
Property of Penn Engineering | 3
Debugging Support in Eclipse
• Eclipse allows you to start a Java program in Debug mode
• Eclipse provides a Debug perspective, which gives you a pre-configured set of views
• Eclipse allows you to control the execution flow via debug commands
Property of Penn Engineering | 4
Setting Breakpoints
• To define a breakpoint in your source code, right-click in the left margin in the Java editor and
select “Toggle Breakpoint”
• You can also double-click on this position in the margin to toggle a breakpoint
Property of Penn Engineering | 5
Setting Breakpoints
• For example, here we’ve set a breakpoint on the line:
int max = 10;
Property of Penn Engineering | 6
Starting the Debugger
• To debug your application, do the following:
- Select your program (Java file) in the Package Explorer
- Go to “Debug As” • “Java Application”
• You can also use the Debug button in the Eclipse toolbar
Property of Penn Engineering | 7
Starting the Debugger
• Eclipse might ask you if you want to open the Debug perspective once a stop point is reached
- Click “Switch”
Property of Penn Engineering | 8
Controlling the Program Execution
• Eclipse provides buttons in the toolbar for controlling the execution of the program you are
debugging
• The Step Into (F5) button executes the currently selected line and goes to the next line in your
program
- If the selected line is a method call, the debugger steps into the associated code
Property of Penn Engineering | 9
Controlling the Program Execution
• Eclipse provides buttons in the toolbar for controlling the execution of the program you are
debugging
• The Step Over (F6) button steps over the call
• This means it executes a method without stepping into it in the debugger
Property of Penn Engineering | 10
Controlling the Program Execution
• Eclipse provides buttons in the toolbar for controlling the execution of the program you are
debugging
• The Step Return (F7) button steps out to the caller of the currently executed method
- This finishes the execution of the current method and returns to the caller of this method
Property of Penn Engineering | 11
Controlling the Program Execution
• Eclipse provides buttons in the toolbar for controlling the execution of the program you are
debugging
• The Resume (F8) button tells the Eclipse debugger to resume the execution of the program code
until it reaches the next breakpoint
Property of Penn Engineering | 12
Controlling the Program Execution
• The Debug view shows the parts of the program which are currently executed and how they
relate to each other
Property of Penn Engineering | 13
Evaluating Variables in the Debugger
• The Variables view displays variables with their current values
Property of Penn Engineering | 14
Evaluating Variables in the Debugger
• The Variables view displays variables with their current values
• Use the drop-down menu in the top-right of the Variables view to display static variables
Property of Penn Engineering | 15
Evaluating Variables in the Debugger
• You can customize the displayed columns
Property of Penn Engineering | 16
Evaluating Variables in the Debugger
• You can customize the displayed columns
• For example, you can show the “Actual Type”
of each variable declaration
Property of Penn Engineering | 17
Evaluating Variables in the Debugger
• The Variables view now displays the “Actual Type” for each variable
Property of Penn Engineering | 18
Returning to Java Perspective
• To go back to the Java perspective (and exit the Debug perspective), click the Java button on the
right of the Eclipse toolbar
Property of Penn Engineering | 19
Debugging Examples - People
Property of Penn Engineering | 20
Person Class
Property of Penn Engineering | 21
Person Class
Property of Penn Engineering | 22
Person Class
• Start debugging using the Java stack trace displayed in the console
- It contains hyperlinks for jumping to particular source code locations
Execution of code leading to an Exception starts here
Property of Penn Engineering | 23
Person Class
• Add a breakpoint and debug in the Debug perspective …
Property of Penn Engineering | 24
Person Class
• … then fix the bug by creating and adding at least one person
Property of Penn Engineering | 25
Person Class
• Start debugging again using the Java stack trace displayed in the console
Execution of code leading to an Exception starts here
Property of Penn Engineering | 26
Person Class
• Add a 2nd breakpoint and debug in the Debug perspective …
Property of Penn Engineering | 27
Person Class
• … then fix the bug by setting the person’s name
Property of Penn Engineering | 28
Person Class
Property of Penn Engineering | 29
Debugging Examples – Syllable Counter
Property of Penn Engineering | 30
SyllableCounter Class
Property of Penn Engineering | 31
Word Class
Property of Penn Engineering | 32
Word Class
Property of Penn Engineering | 33
Word Class
Property of Penn Engineering | 34
Word Class
Property of Penn Engineering | 35
Word Class
Property of Penn Engineering | 36
Word Class
Property of Penn Engineering | 37
SyllableCounter Class
• Run the program and type a sentence ending with a period
• Note the output:
- Every word lost its last character
Property of Penn Engineering | 38
SyllableCounter Class
• Add a breakpoint and debug in the Debug perspective …
- Step Into the getText method to see what’s happening
• Note: By default, Step Into will
enter the first method it finds on
the line
• Step Into will first enter the println method. You’ll have to Step Return out of the println
method, then Step Into the getText method.
Property of Penn Engineering | 39
SyllableCounter Class
• Now, add a new breakpoint where the instance of Word is created …
- Step Into the Word constructor
• Note: By default, Step Into
will enter the first method
it finds on the line
• Step Into will first enter a loadClass method in the ClassLoader. You’ll have to Step Return out of
the loadClass method, then Step Into the Word constructor.
Property of Penn Engineering | 40
Word Class
• Step Over each line in the Word class constructor
Property of Penn Engineering | 41
Word Class
• … then fix the bug by correctly setting i and j
Property of Penn Engineering | 42
SyllableCounter Class
• Run the program and type a sentence ending with a period
• Note the output:
- The syllables are counted incorrectly
Property of Penn Engineering | 43
SyllableCounter Class
• Add a breakpoint and debug in the Debug perspective …
- Step Into the countSyllables method to see what’s happening
Property of Penn Engineering |
Word Class
• … then fix the bug by correctly resetting the insideSyllable variable
Property of Penn Engineering | 45
SyllableCounter Class
• Run the program and type a sentence ending with a period
• Note the output:
- It’s not perfect, but it’s getting there!
Property of Penn Engineering | 46