How to debug a Java program?
When everyone first learned Java, they would think that IDE debugging is very high-end, but it is actually very simple.
The following article will teach you how to debug in Eclipse as simply and intuitively. Other IDE debugging steps are similar.
1. Set breakpoints where you think there is something wrong.
Before the number of lines of code, right-click, note that it is right-click, and then select Toggle Breakpoint.
You might ask, how do I know where to place breakpoints?
If you have no feeling about this problem, you can hit a few more breakpoints and step in debugging until the exception is found, just take a little more time, and this will give you a deeper understanding of the execution process of the program!
Of course, if you can roughly go to where there may be a problem, or the exception information reports class location, you can set a breakpoint here.
2. Click Debug. If it is a web program, you need to start Tomcat or Apache server in Debug mode.
This is very important. The standard Start mode cannot enter preset breakpoints, which cannot achieve the purpose of debugging.
3. Run the program. When the program runs to the position where the breakpoint was just set, it will stop, and the background color of that line of code will be highlighted.
At this time, you can control the program through the screen button or keyboard.
Below are the shortcut keys for the corresponding debugging of the keyboard. If it does not work, you can check if it is a keyboard conflict.
For example, the shortcut keys of Youdao Dictionary often conflict with Resume in Debud mode.
Scope function shortcut keys
Global single step returns to F7
Global single step skip F6
Global single step jump into F5
Global single step jump into select Ctrl+F5
Global debugging last started F11
Continue F8 globally
Globally use filters to step through Shift+F5
Add/remove breakpoints globally Ctrl+Shift+B
Global display Ctrl+D
Global run last started Ctrl+F11
Run globally to line Ctrl+R
Global execution of Ctrl+U
4. Enter the debugging interface to see the information you want.
5. You can view the values of all variables in Variables, such as the values in the breakpoint you just set, right-click ChangeValue to change, and some IDEs support hot changes in the window and executing code.
6. The first button below is to enter the method execution. For example, if you call other methods, you can enter the method to execute step by step. If you click the second button, it will only be executed step by step within this method. When the third button is used, you will jump out of this method and continue to execute the original method that calls this method. The description is as follows.
7. Complete the program.
8. Add decompilation plugin for Eclipse for better debugging
Generally speaking, our project will more or less reference some external jar packages. If we can view the source code of the jar package, it can be said that our debugging will be twice the result with half the effort.
1. Download and install jad.exe. Unzip jad.exe into the program directory (can place any directory), for example: C:/Program Files/Jad/jad.exe.
2. Install the jadclipse plug-in. Download and unzip net.sf.jadclipse_3.3.0.jar, copy it to the eclipse/plugins directory, and restart eclipse.
3. Configure jadclipse. Under the eclipse window, click Window > Preferences > Java > JadClipse > Path to Decompiler.
(Set the absolute path of jad, for example C:/Program Files/Jad/jad.exe)
You can check the Use Eclipse code formatter (overrides Jad formatting instructions) option, which can be consistent with the code style formatted by Ctrl+Shif+F.
After performing these steps, click to view on the class or method imported from the Jar package to view the source code. If not, refer to the following solution:
In most cases, eclipse fails to automatically set JadClipse Class File Viewer to the default opening method of class files.
In Eclipse's Windows->Perference->General->Editors->File Associations, the editor associated with "*.class" and "*.class without source" is modified by default to "JadClipse Class File Viewer".
I have configured the jad plug-in several times. If it cannot be decompiled, it has been tried and tested after such a setting.
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.