Basic knowledge of Java Debug debugging

Source: Internet
Author: User
Tags ming

reprint: http://iamzhongyong.iteye.com/blog/1397374

Debug view

Know the debug view, the red part is the thread stack view, the yellow part is the expression, the breakpoint, the variable view, the blue part is the code view.


Thread Stack View

Describe the meanings of these buttons separately:

1. Indicates that the current implementation continues to run until the next breakpoint, and the shortcut key is F8.

2. Interrupt the entire process

3. The current method is entered, and the shortcut key is F5.

4. Indicates that the next line of code is running, and the shortcut key is F6.

5. Means to exit the current method, return to the call layer, and the shortcut key to F7.

6. represents the stack of the current thread, from which you can see what code is running, and the entire calling procedure, along with the code line number



Variable view

1. For the variable name view, displays all accessible instance variables and local variables in the current line of code

2. Show all the variable values

3. Variable values can be changed by this window



Breakpoint View

1. Show All Breakpoints

2. Disable the selected port in the current window 1 and click Enable again.

3. Exception Breakpoint



An expression view

Expression view: The expression view is a more common view of the debug process, you can observe some of the variables you are interested in, you can add some of your own expressions, or you can see the results of a line of code running.

1. Expressions

2. Click this to add an expression



Code view

Code view: Used to display the specific code. Where the green part refers to the code that is currently going to be executed


Scene One: Xiao Ming hard Two days finally will his responsibility of the task completed, the next day after the test to find Xiao Ming said, Xiao Ming's program has a bug, can be Xiao Ming after careful debugging, found that there is no problem, but the test environment does have problems, so xiaoming encountered a problem, testing the environment of Linux, And can not go to Linux to debug, xiaoming this time want to be Linux can also debug just fine.

Remote Debug

Remote debug: Remote Debug, as the name implies, is able to debug any Java on the remote operating system, but only if there is a local need for synchronized code.

1. the step of remote debug is to add a special when starting the Java process on the remote operating system

-xdebug-xnoagent-djava.compiler=none-xrunjdwp:transport=dt_socket,address= $DEBUG _port,server=y,suspend=n

2. create a new remote Java application in Eclipse

Remote Debug

1. Open the Debug Configurations view

2. Right-click on the remote Java application,

New

3. Select Source Engineering

4. Enter remote IP and port, port as service

End of the $debug_port, click OK.



Scene One: Xiao Ming wrote a task performer, the performer of the execution of a number of tasks, in the network after a period of time, found that there is a probability of some failures, found that after a period of time, the task of abnormal exit, exit because it is a null pointer, can xiaoming want to debug in the local, Do not know where to hit the breakpoint, the problem is the probability of events, not necessarily appear, so xiaoming debug several times down, dizzy, even code are not clear, Xiao Ming want to have a breakpoint whenever there is a null pointer when the time to stop to let him find the problem, that much good ah.

Exception Breakpoint

Exception breakpoint: In the process of locating the problem, often encounter breakpoint can not hit the right place, so that and the issue of the trigger point is very different, so this time may wish to try an exception breakpoint, as the name implies, the exception breakpoint refers to throw an exception after the automatic suspend breakpoint.

Click on the red area to add an exception breakpoint



Enter the type of exception you want to locate, such as nullpointerexception, so that when any NullPointerException exception is thrown in the system, the current thread will be suspended, giving you the opportunity to locate the problem.

Scene One: Xiao Ming wrote a huge cycle, in the debugging code, Xiao Ming found that whenever the cycle to the 100,000th time, is the problem, did not reach their expectations, so Xiao Ming in the loop hit a breakpoint, want to see what is going on, but Xiao Ming never thought, How difficult it is to get to 100,000 cycles, Xiaoming has begun to think at this time if he can have such a breakpoint:

If loop Number = = 100000, thread stops

Conditional breakpoint

As shown on the right, loop 1000 times if you want to loop to 500

Time to stop, you can create a conditional breakpoint, right

Click on the breakpoint to hang the breakpoint Properties.



Check Enable Condition

In the space, add your own condition, and if the condition returns True, the thread is suspended and if False, the exception is ignored

Hit count is the number of times that the breakpoint has passed, formally suspending the thread, and if set to 500, then the first 499 times, after which the breakpoint will not stop, and when the No. 500 time, the breakpoint suspends the current thread.

An expression

The expression can look at some command lines that are not in the current code, which makes it easy to locate the problem.

Scene One: Xiao Ming recently encountered a problem, in calling a third-party plug-in will always have problems, xiaoming suspect is a third-party plug-in bug, but Xiaoming did not find the source code can not be debug, Xiao Ming how to do?

Debug location of third-party plug-in issues

1. Using the Anti-compilation tool to decompile the code

2. Filter the anti-compiled source code

3. Fix source code compilation error

4. Perform the Debug

Debug some Experience

1. Minimize debug, less debug, good programmers always spend 80% of the time to think about how to solve problems, 20% of the time to do the code, and bad programmers always spend 20% of the time to write code, 80% of the time to debug the code, before you try to think how to do, and has done a full experiment for your own ideas.

2. As much as possible to improve the efficiency of debug, set the appropriate breakpoints, the use of shortcut keys.

3.debug F6 shortcut keys are often used, it and PowerWord shortcut keys conflict, so in the debug when the best will be powerword off.

An expression of 4.debug is executable code that will have a permanent effect on the results of the code, and when debugging, it is often clear that the unused expressions are removed.

Basic knowledge of Java Debug debugging

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.