Without errors, debugging, and Eclipse being the most common Java IDE, Eclipse has the skills to improve the efficiency of our debugging. This article is translated from "Top Java debugging Tips with Eclipse." Believe me, the debugging techniques here will give you more time to do other things (dating).
This blog will add some personal understanding, you can click here to view the original. Limited to my english level, translation may not be very accurate, please forgive me.
In this tutorial, we'll see techniques for debugging Java applications using Eclipse. The debugger helps us find and modify bugs in the program. Here we concentrate on Run-time errors without mentioning compile-time errors. In this tutorial, we will focus on our favorite Ide:eclipse GUI debugger without mentioning the command-line debugger. While we are talking about eclipse, the techniques for debugging are generic, and these techniques apply to most Ides, like NetBeans and so on.
Before you start reading this tutorial, it is helpful for you to take a look at this Eclipse shortcut (English). My eclipse version is Luna (the original is Juno). Do not use SYSTEM.OUT.PRINTLN as a debugging tool. Enable verbose logging levels for all involved components. Use Log Analyzer to read logs. 1. Conditional Breakpoint
You should all know how to add a breakpoint: Double-click the line number and its left. In the debug Perspective (Debug Perspective), the Breakpoints view lists all breakpoints. We can add a conditional expression to the breakpoint. That is, the breakpoint is activated only when the expression is ture, and the execution of the program is paused. Otherwise, the breakpoint will temporarily fail.
Let's look at an example:
Int[] A = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0};
for (int i=0; i<a.length; ++i) {
System.out.println ("21%a[" + i + "] =" + (21%a[i));
}
1 2 3 4 5 1 2 3 4 5
Run this program will find that always in the output 21%A[8] = 3 after the error, if you add a breakpoint directly in line 4th, then we need to click the mouse 9 times to find out, the original is because of a[9]=0. If we set the i==9, then the breakpoint only works if I 9 o'clock. Which is to suspend the program. Will save a lot of trouble:
The following illustration shows a break (here is i==9) when the setting satisfies the condition. Note the asterisk on the left side of the breakpoint, which means that no save, that is, the changes will not take effect, you can ctrl+s save, or wait for it to automatically save
Or you can: set to take effect the 10th time after the breakpoint, and then set the breakpoint to be invalid
After mastering these skills, you will never encounter a bit of crazy after the mouse accidentally point over, can only restart the embarrassment of debugging. 2. Exception Breakpoint
There is a button in the breakpoints view that looks like j!, and we can use this button to add a breakpoint based on a Java exception. For example, we want this program to encounter NullPointerException
Pause and accept debugging, then we can use this button to add a breakpoint:
Note: The Luna version of Eclipse appears to have supported automatic interrupts at runtime exceptions. The example above, even without any breakpoints, is halted when the i==9 is encountered. However, it is also necessary to manually add breakpoints to check for exceptions. 3. Observation Points
This is a feature that I like very much. Suspends program execution when the selected variable is accessed or modified, and accepts debugging. The way to add an observation point is to select Outline view, right-click on the member variable that adds the observation point, and select toggle Watchpoint (Toggle observation point). This creates an observation point for this property, and the observation point is listed in the breakpoint view. This is especially useful when you feel that the attribute is modified for no reason and cannot be found.
Currently, only the properties of a class can add an observation point, and a variable in the normal method cannot add an observation point. In fact, there is no need. 4. Calculate and display
Pressing CTRL+SHIFT+D or Ctrl+shift+i on a selected variable or expression while debugging displays the variable or the value of the expression. We can also add a permanent watch on an expression/variable, which displays the variable/expression to the expression view: Right-click on the variable –> view.
Note: For variables, put the mouse directly up, stay for a while will appear the value of the variable.
5. Change the value of a variable
When debugging, we can change the value of the variable. Select the value of a variable in the variable view, enter a new value, return, and complete the modification! You can change a variable that doesn't match our expectations to the value we expect, and continue running.
6. Pause at Main method
In the Run/debug setting, we can select the "Stop in main" check box, and if we enable this feature, the program will pause at the first line of the main method when debugging. You can then right-click the line that needs to be paused and select Run to lines (ctrl+r Run to the line, which is equivalent to creating a temporary breakpoint that eliminates the hassle of deleting breakpoints.
7. Environment Variables
We can easily add environment variables by editing the configuration dialog box, instead of-> properties through my Computer ... This has the advantage of not affecting the overall computer environment variables, only the Java applications you configure are affected. So, if there are any environment variables that are only needed by this program, then modify it here without changing your system variables to clutter.
8.Drop to Frame
This second feature I like very much. The Drop to frame means that you can jump back to the beginning of a method in the call stack (note: Except for the first method, the Main method), and the value of all the context variables goes back to that time. You can quickly revert to a state without needing to restart debugging. Of course, the side effects of the original execution are irreversible, such as inserting a record into the database.
Select the name of the method you want to return to, click the Drop to Frame button on the Debug toolbar, or right-click –>drop to frame
9.Step Filtration
When we use one step into (F5) function, we may accidentally enter a library that we don't want to enter, such as the Java Class Library. We can add a filter to the preferences and exclude the specified package. Then we will not go into the classes in these packages when we use Single-step entry.
10. Enter, Skip and return
These techniques are debug's most basic and important knowledge f5-stepping into, move to the next step, if the current row is a method call, the first line of the method is f6-skipped, moved to the next, and if the current row has a method call, the methods are executed directly and returned, and then to the next F7-jumps out of the function and continues execution until the current method completes or when a breakpoint is encountered, stopping f8-execution to the next breakpoint.
Translated in 2015/04/09