Top 10 Java debugging tips with eclipse (10 tips for debugging Java in eclipse)

Source: Internet
Author: User

In this tutorial, we will see debugging Java applications using eclipse. Debugging helps us identify and solve defects in applications. We will focus on the running time, rather than the compile-time error. A command line debugger like GDB is provided. In this tutorial, we will focus on Gui-based debugging. We will run our favorite ide eclipse through this tutorial. Although we talk about eclipse, most of the points are generic, applicable to debugging ide like netbeans.

Before reading this article, I recommend that you take a look at eclipse
Shortcut Key manual. You can also download the PDF file here. My eclipse version is 4.2.
Juno.

  • Do not use system. Out. println as a debugging tool
  • Enable detailed logging levels for all components
  • Use a log analyzer to read logs
1. Conditional breakpoint

Imagine how to add a breakpoint. Generally, double-click the left side of the row number. In the debug view, the breakpoint view lists all breakpoints, but we can add a Boolean condition to determine whether the breakpoint is skipped. If the condition is true, the program stops at the breakpoint. Otherwise, the breakpoint is skipped and the program continues to run.

2. Exception breakpoint

There is one in the breakpoint view that looks like J! You can use it to add an exception-based breakpoint. For example, if we want the program to suspend when nullpointerexception is thrown, we can:

3. Observation Point

This is a good function. It allows the program to pause and debug when a selected property is accessed or changed. The simplest way is to declare the member variable in the class by double-clicking on the left of the statement line number to add an observation point.

4. View Variables

Run Ctrl + Shift + D or Ctrl + Shift + I on the selected variable to view the variable value. In addition, you can add monitoring in the expressions view.

5. Change variable value

We can change the value of the variable in debug. You can perform operations as shown in variables view.

6. Stop in the main method

In the run/debug settings, we can enable this feature as shown in. The program stops at the first line of the main method.

7. Environment Variable

We can easily add environment variables in the edit conriguration dialog box.

8. Jump out of the function to the selected Layer

This feature is very cool and is my second favorite feature. Drop to frame means that you can re-execute it at the beginning of the current method, and the values of all context variables return to that time. Not necessarily the current method. You can click any frame in the current call stack to jump there (except the first frame ). The main purpose is to quickly restore the state of all variables to the beginning of the method and re-execute it, that is, you can perform multiple debugging in the context you are concerned about over and over again (combined with other functions such as variable value change) without re-debugging. Of course, the side effects of the original execution process are irreversible (for example, you have inserted a record into the database ).

9. Step-by-step filtering

When we press F5 during debugging, it will enter the internal method, but this has a disadvantage that sometimes it may enter the internal library (such as JDK ), it may not be what we want. We can add a filter in preferences to exclude the specified package.

10. Jump in, skip, and return

In fact, this technique is the most basic knowledge of debug.

  • F5-Step into: Move to next, if the current row is a method call, it will enter the first line of this method. (This can be ruled out through Article 9)
  • F6-Step over: Move to the next row. If the current row has a method call, this method will be returned after execution, and then to the next row.
  • F7-Step return: continues to execute the current method, and when the current method is executed, the control will go to the row that the current method is called.
  • F8-Move to the next breakpoint.


From: http://javapapers.com/core-java/top-10-java-debugging-tips-with-eclipse/
Related Article

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.