For debugging we will create an example project. Create a Java project with the name
de.vogella.combug.first
and add
the package
de.vogella.combug.first
. Also create
the following classes.
package de.vogella.combug.first; public class Counter { private int result = 0; public int getResult() { return result; } public void count() { for (int i = 0; i < 100; i++) { result += i + 1; } } }
package de.vogella.combug.first; public class Main {/** * @param args */public static void main(String[] args) { Counter counter = new Counter(); counter.count(); System.out.println("We have counted " + counter.getResult()); } }
To set
breakpoints right click in the small left margin in your
source code editor
and select
"Toggle
Breakpoint". Or you can
double-click
on this position.
For example in the following screenshot we set an breakpoint on the line
For example in the following screenshot we set an breakpoint on the line
Counter counter = new Counter();
.
To debug your application, select a Java file
which contains
a
main
method, right click on it and select
→ .
If you have not defined any breakpoints, this will run your program as normal. To debug the program you need to define breakpoints.
If you start the debugger the first time, Eclipse asks you if you want to switch to the debug perspective. Answer "yes". You should then see a perspective similar to the following.
You can use F5 / F6, F7 and F8 to step through your coding. The meaning of these keys is explained in the following table.
These commands are also available in the toolbar of Eclipse. The following picture displays the buttons and their related keyboard shortcuts.
If you have not defined any breakpoints, this will run your program as normal. To debug the program you need to define breakpoints.
If you start the debugger the first time, Eclipse asks you if you want to switch to the debug perspective. Answer "yes". You should then see a perspective similar to the following.
You can use F5 / F6, F7 and F8 to step through your coding. The meaning of these keys is explained in the following table.
Table 1. Debugging Key bindings
Command | Description |
---|---|
F5 | Goes to the next step in your program. If the next step is a method / function this command will jump into the associated code. |
F6 | F6 will step over the call, i.e. it will call a method / function without entering the associated code. |
F7 | F7 will go to the caller of the method/ function. This will leave the current code and go to the calling code. |
F8 | Use F8 to go to the next breakpoint. If no further breakpoint is encountered the program will run normally. |
These commands are also available in the toolbar of Eclipse. The following picture displays the buttons and their related keyboard shortcuts.
The call stack show the parts of the program which are currently
executed and how they relate to each other. The current stack is
displayed in the
"Debug"
View.
The
Variables
Ciew
displays fields and local variables from
the
current stack. Please note
you need to run the debugger to see the
variables in this
View.
Use the drop-down menu to display static variables.
Via the drop-down menu you can also customize the displayed columns. For example, you can show the actual type of each variable declaration. For this select → → .
Another nice feature is the "New Detail Formatter" in which you can define how a variable is displayed. For example the
Add the following code to get the current value of result variable in the
Use the drop-down menu to display static variables.
Via the drop-down menu you can also customize the displayed columns. For example, you can show the actual type of each variable declaration. For this select → → .
Another nice feature is the "New Detail Formatter" in which you can define how a variable is displayed. For example the
toString()
method in our
Counter
class
shows something meaningless, e.g.
de.vogella.combug.first.Counter@587c94
. Right mouse on the
→ .
Add the following code to get the current value of result variable in the
Counter
class of this example.
The following section shows advanced options for debugging. It
is not
based on the example introduced previously.
If you want to temporary de-activate all your breakpoints you
can press the button "Skip all breakpoints". This button is visible,
if you
select the "Breakpoints" tab.
If you press this button again, your breakpoints will be reactivated.
If you press this button again, your breakpoints will be reactivated.
After setting a breakpoint you can select the
properties of the
breakpoint, via
→ .
You can define a condition that
restricts when the
breakpoint
will
become active.
For example, specify that the breakpoint should only be active after it has been reached 12 or more times (Hit Count).
Or, you can put in a conditional expression (which you can also use for logging). Execution will stop at the breakpoint, if the condition evaluates to true.
For example, specify that the breakpoint should only be active after it has been reached 12 or more times (Hit Count).
Or, you can put in a conditional expression (which you can also use for logging). Execution will stop at the breakpoint, if the condition evaluates to true.
A watchpoint is a breakpoint set on a field. The debugger
will
stop whenever that field is
read or changed.
You can set a watchpoint by double-clicking on the left margin, next to the field declaration. In the properties of a watchpoint you can define if the execution should stop during read access (Field Access) and during write access (Field Modification).
You can set a watchpoint by double-clicking on the left margin, next to the field declaration. In the properties of a watchpoint you can define if the execution should stop during read access (Field Access) and during write access (Field Modification).
You can also set breakpoints which are triggered when exceptioins
are
thrown. To define an exception breakpoint click on the "Add Java
Exception Breakpoint"
icon in the "Breakpoints"
View
toolbar.
You can define if you want to stop for caught and / or uncaught exceptions.
You can define if you want to stop for caught and / or uncaught exceptions.
A method breakpoint is defined by double-clicking in the left
margin of the editor next to the method header.
You can define if you want to stop the program before entering or after leaving the method.
You can define if you want to stop the program before entering or after leaving the method.
A Class Load Breakpoint will stop when the
class is loaded.
To set a class load breakpoint, right-click on a class in the Outline View and choose "Toggle Class Load Breakpoint".
To set a class load breakpoint, right-click on a class in the Outline View and choose "Toggle Class Load Breakpoint".
For every breakpoint you can define a hit
count in it's
properties. The application is stopped once
the
breakpoint is reached
the number of times defined in the hit
count.
Eclipse allows you to select any level (frame) in the call stack
during debugging and set the JVM to restart from that point.
This allows you to rerun a part of your program. Be aware that variables which have been modified by code that already run will remain modified.
Changes made to variables or external data, e.g. files, databases, will not be reset when you drop to a previous frame.
To use this feature, select a level in your stack and press the "Drop to Frame" button in the toolbar of the "Debug" View.
For example you can restart your "for" loop. The field "result" will not be reseted in this case.
This allows you to rerun a part of your program. Be aware that variables which have been modified by code that already run will remain modified.
Changes made to variables or external data, e.g. files, databases, will not be reset when you drop to a previous frame.
To use this feature, select a level in your stack and press the "Drop to Frame" button in the toolbar of the "Debug" View.
For example you can restart your "for" loop. The field "result" will not be reseted in this case.
No comments:
Post a Comment