It can help identify and solve application defects. In this article, the author will use Eclipse, a commonly used development tool, to debug Java applications. However, the debugging methods introduced here are basically universal and are also applicable to NetBeans IDE. We will focus on the runtime.
Before starting, I recommend you to read the article Eclipse shortcuts, which will bring you a lot of convenience. In this article, the Eclipse Juno version (Eclipse 4.2) is used. I would like to give you 3 suggestions before starting!
Do not use System.out.println as a debugging tool. Enable logging levels for all involved components and use a log analyzer to read the logs.
1.Conditional breakpoint
If you don't know how to add a breakpoint, just click on the left panel (before the line number) and the breakpoint will be created. In the debugging interface, the "Breakpoints" view will list all created breakpoints. We can add a Boolean condition to it, that is, the breakpoint will be activated and if the Boolean condition is true, the breakpoint will be executed, otherwise it will skip execution.
2.Exception breakpoint
In the breakpoint view, there is a J! mark button! We can use this button to add a Java exception breakpoint. For example, if we want the program to still be able to continue debugging when it encounters a NullPointerException, then we can use this button to add an exception breakpoint!
3. Monitoring points
This is a very nice feature that stops the program execution and allows debugging when a selected property is accessed or modified. Select a class variable in the Outline view and select Switch Watchpoint from the context menu. The attribute watchpoint will be created, and all watchpoints will be displayed in the form of a list in the Breakpoints view.
4.Assessment/Inspection
Press Ctrl+Shift+D or Ctrl+Shift+I to display the value of the selected variable or expression. We can also add permanent watchpoints to a variable or expression. When the program is debugged, these watchpoints will be displayed in the Expression view.
5. Modify variable value
During debugging, we can modify variable values. First select a variable and then enter the Variables view, and enter the value in the corresponding Value column according to the variable type.
6. Stop execution in the Main function
In the run/debug settings, there is a "Main" tab in the edit configuration dialog box, and we can check the "Stop in main" checkbox. If selected, when debugging a Java program based on the main method, the program will stop execution at the first line of the main method.
7. Environment variables
Instead of adding environment variables in system properties, we can easily add them in the Edit Configuration dialog box.
8.Drop to Frame
This is also one of my favorite features. During debugging, you can jump back to the beginning of the call stack frame and the variable values will return to the beginning. Adjust the depth of the stack according to the rollback. The main purpose of this function is to quickly return the status of all variables to the state when the method started to be executed. Then you can re-execute it over and over again, so that you can debug multiple times where you are concerned. However, there will also be some side effects during the execution process. For example, the data inserted into the database cannot be deleted!
9. Distribution filtering
When we enter the (F5) method, we can also access its external library (such as java.*). We may not need this library, so we can add a filter to the Perference tab page to exclude this package.
10. Enter, exit and return
I leave this for the last point, these are the things that must be understood (and preferably mastered) during debugging:
F5 - Enter: Move to the next step. If there is a method call in the current line, the control will jump to the first line of the called method for execution.
F6 - Jump out: move to the next line. If there is a method call in the current line, it will move directly to the next line for execution. It will not enter the called method body.
F7 - Return: Jump out of the current method and continue execution.
F8——Move to the next breakpoint for execution.