Java Common Debugging techniques

Source: Internet
Author: User

  Debugging can not only find the application defects, but also solve the defects. For Java programmers, they not only have to learn how to develop decent programs in eclipse, but they also need to learn how to debug programs. This article describes the Java programmer must know the 10 debugging skills, to ensure that you benefit!

Debugging can help identify and resolve application flaws, and in this article, the author will use the Common development tool eclipse to debug Java applications. But the debugging methods described here are basically generic and apply to NetBeans IDE, where we focus on the runtime.

Before you begin, it is recommended that you take a look at the Eclipse shortcuts article, which will bring you a lot of convenience. The Eclipse Juno version (Eclipse 4.2) is used in this article to give you 3 tips before you start!

    1. Do not use SYSTEM.OUT.PRINTLN as a debugging tool
    2. Activate and use all of the component log levels involved
    3. Use Log Parser to read logs

1. Conditional breakpoints

If you don't know how to add a breakpoint, just click on the left panel (preceded by the line number) and the breakpoint is created. In the debug interface, the Breakpoints view lists all the breakpoints that were created. 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 Breakpoints view, there is a j! tag button! We can use this button to add a Java exception breakpoint. For example, we want the program to continue debugging when it encounters a null pointer exception (nullpointerexception), so we can use the button to add an exception breakpoint!

3. Monitoring points

This is a very good feature when the selected property accesses or modifies the program, and the program stops execution and allows debugging. Selecting a class variable in the outline view and selecting toggle Watch from the context menu will create a property watch that will be displayed in the breakpoint (breakpoints) view as a list of all the watch points.

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 observers to a variable or expression, and when the program is debugging, the observers are displayed in an expression view.

5. Modifying variable values

During the debugging process, we can modify the value of the variable. Select a variable and then enter the variable view (Variables view), and enter the value in its corresponding value joins according to the variable type.

6. Stop execution inside the main function

In the Run/debug settings, the Edit Configuration dialog box has the "main" tab, and we can tick the "Stop in Main" check box. If selected, when debugging a Java program based on the main method, the program stops executing at the first line of the Main method.

7. Environment variables

Instead of adding environment variables to System properties, we can easily add them in the Edit Configuration dialog box.

8.Drop to Frame

This is one of my favorite features. During debugging, you can jump back to the beginning of the call stack frame and the value of the variable will return to the original. Depending on the depth of the stack, the main use of this feature is that all variable states can quickly return to the way the method starts executing, and then you can do it over and over again, so that you can debug multiple times in your area of interest, but there are some side effects during execution. For example, the data inserted into the database can not be deleted!

9. Distribution filtering

When we enter the (F5) method, we can also access its external libraries (such as java.*), we may not need this library, we can add a filter on the Perference tab page to exclude this package.

10. Entering, jumping and returning

I put this on the last point, during the commissioning process, these are things that must be understood (preferably mastered):

f5--Enter: Moves to the next step, if the current row has a method call, the control jumps to the first line of the called method execution.

f6--jumps: Moves to the next line. If there is a method call on the current line, it is moved directly to the next line. Does not enter inside the called method body.

f7--returns: Jumps out of the current method and continues down execution.

f8--moves to the next breakpoint to execute.

Java Common debugging techniques (GO)

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.