Tips for debugging Java programs with Eclipse

Source: Internet
Author: User

You should have seen some popular posts like "N Things about debugging". Suppose I spend 1 hours a day debugging my application, and that's a lot of time to build up. For this reason, use these times to value and understand all the features that make our debugging more convenient. It will save you some time and make your life more comfortable and relaxed. It also shows that other posts on this topic are valuable.

1th: Do not debug too much

A crazy statement about debugging as the opening. But it has to be said! Try slicing your complex logic into multiple separate units and writing unit tests to detect the correctness of your code. I imagine a process like this should happen very often----some people click through a large Web application, fill in multiple forms, switch to different pages, examine the computational logic on the last page, and implement most of the logic in this debug view. Always ask yourself before starting your tomcat: Is there a way to detect these behaviors using a unit test? You may not have known or forgotten about these times in the past, but from now on, we will focus on some of the debugging techniques of Eclipse, and you will find a lot of good things outside of good code design.

-breakpoints View: Conditional breakpoint

This feature is useful if you are only interested in a part of the app. For example, if you want to check the program during the 13th cycle, or debug some functionality in an abstract parent class, you are only interested in one of the specific implementations. You can set the conditions in the breakpoints view or the right-click menu ("Breakpoint Properties") labeled by the Blue breakpoint next to the code. You can pause the program when the conditional code fragment is true, or suspend the program when the value of the code fragment changes.


Enixyu
Translated 3 years ago

2 Human Top

top   Good translation!

other translations (1)

-variable view: Show Logical Structure

If you need to see the values of a Map object or list object in a variable view, it is generally not as easy for Eclipse's default settings. Assuming that you are using HashMap, you need to click through the individual entity entries and face various HashMap implementation details. However, there is a button called "Show Logical Structure" above the variable view. It is very useful, especially if the information displayed by the ToString () method of your object is not friendly. My boss showed me this feature a couple of weeks ago. You know, he often deals with PowerPoint or Excel. How humiliating for a developer like me.  

Does not turn on show Logical Structure  
 
turns on show Logical Structure  
& nbsp

Enixyu
Translated 3 years ago

3 Human top

top   translation is good Oh!

-variable View: Change the value of a variable ... /strong>

When you need to change the input information slightly, you do not need to restart the debugging session, just enter the new information in a table, you can directly modify the value of your variable in the debugging phase. There are times when you can save some time, you can simulate some weird situations through this function more simply.

-Display view

Do you know the display view? You can activate it through "window", "Show View", "Display" during debugging. Your eclipse should now be a new, blank view. You can use this view to enter or calculate some new code. The code is executed in the context of the current debug location, which means that you can use all variables or even content assistants. To execute your code, simply mark it and use the right-click menu or Ctrl+u (execute) or  ctrl+shift+i (check).

Enixyu
Translated 3 years ago

2 Human top

P class= "vote" > top   translation Good Oh!

-navigation: Drop to Frame

I want to be Everyone knows "step into", "Step Over", and may also know "step return". These are the basic navigation features when debugging. I'd like to mention two advanced methods of navigating that I really like. The first one is "Drop to Frame". With this feature you can fall back in time   you can directly fall back to a frame in a running Java stack frame. When I debug, I accidentally miss a line of attention, I often use this fallback one frame. Using the Drop to frame feature, I can simply rerun a frame of code.

 

-navigation: Step into Selection

The second one is "Step into Selection". It's also very simple, but a lot of people use one. To use it, you just press Ctrl+alt and click on the name of the method you want to run to. Very convenient, very fast. Compared to "step into" this is very convenient, think you like tracking into a function with a lot of parameters, if you use "Step into" you must enter, exit each parameter calculation process, you can enter into the function you really want to go. "Run To Line" is also a great feature. Just put the cursor in front of which line you want to run and press "Ctrl+r".
 

 

Ljb_iss
Translated 3 years ago

4 people top

top   translation is good!

-Navigator: Using the keyboard

If you avoid using a mouse, you will be more likely to work faster. You should at least have the following shortcut:

  • f5– Step into
  • f6– step and Skip
  • f7– stepping and returning
  • f8– "continue to execute"
  • ctrl+shift+b– "Add Breakpoint"
  • ctrl+shift+i– "Check"

-Breakpoint View: Watchpoints

What happens if I change this variable? Sometimes creating a watchpoint is useful for debugging. The debugger stops, regardless of whether the observed field is modified or read-You can configure the decision yourself. You just double-click a field, and you can see the watchpoint in the breakpoint view, and you can edit its properties. You can even have a number of visits, which means that the debugger stops when the number of times the variable is accessed reaches that amount. This is also true for normal breakpoints.


Enixyu
Translated 3 years ago

3< /em> person top

top   translation is good!

-friendly and readable object

Variable view is a realistic corresponding value using the object's ToString method. For this reason, it can be very useful for debugging if you provide a friendly ToString method implementation. The default ToString implementation of Java.lang.Object in Javadoc is also recommended:

Returns a String representing the object. Usually the <code>toString</code> method returns such a string, "literally" the object. The return result must be concise but rich in information that can represent the object, and is friendly to read. We recommend that all subclasses override this method.

You can refer to  tostringbuilder in Commons-lang. It provides functionality to write a "good and consistent" ToString method (referenced from Javadoc).

Default ToString

 
Default Tostringbuilder example
 
Tostringbuilder Example – multiline text style
 

If you cannot modify the ToString implementation, for example if you are using a framework or an external API, you can have another option to create a "Detail Formatter" in Eclipse. You need to right-click an object in the variables view and click on "New Detail Formatter ...". Then you can enter some code to show the object.

Enixyu
Translated 3 years ago

2 Human top

top   translation is good Oh!

All translations in this article are for learning and communication purposes only, please be sure to indicate the translator, source, and link to this article.
Our translation work in accordance with the CC agreement, if our work has violated your rights and interests, please contact us promptly

Before reading this article, I recommend that you take a look at the Eclipse Accelerator manual , my version of Eclipse is 4.2 Juno.

Three points first.

    • Do not use SYSTEM.OUT.PRINTLN as a debugging tool
    • Enable verbose logging levels for all components
    • Use a Log Parser to read the log

1. Conditional breakpoint

Imagine how we usually add breakpoints, and it's common practice to double-click the left side of the line number. In the debug view, 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 resumes execution.

2. Abnormal Breakpoint

There is one in the breakpoint view that looks like J! button, we can use it to add a breakpoint based on an exception, for example we want the program to pause when NullPointerException throws, we can do this:

3, Observation point

This feature I like very much, he allows the program execution to pause and debug when a selected property is accessed or changed. The simplest way is to double-click the line number of the statement that declares the member variable in the class, and you can add an observer point.

4. View variables

You can see the value of the variable using CTRL+SHIFT+D or Ctrl+shift+i on the selected variable, and we can also add monitoring in expressions view.

5. Change the value of the variable

We can change the value of the variable at debug time. You can do this as shown in variables view.



6. Stop in the Main method in the Run/debug setting, we can enable this feature as shown. The program will stop at the first line of the Main method

7. Environment Variables we can easily add environment variables in the Edit Conriguration dialog box

8. Drop to frame This feature is very cool and is my second favorite feature, Drop to frame means that it can be re-executed at the beginning of the current method, and the values of all the context variables are back to that time. Not necessarily the current method, you can click on any frame in the current call stack to jump there (except for the first frame). The main use is that all variable states are quickly restored to the beginning of the method and re-executed again, so that you can debug it over and over again in the context of your concern (combined with other functions such as changing the value of the variable), instead of having to debug it again. Of course, the side effects of the original execution process are irreversible (for example, you inserted a record into the database).

9. Step Filtration when we debug the F5 will go into the inside of the method, but there is a drawback sometimes may go into some libraries inside (such as the JDK), may not be what we want, we can add a filter in the preferences, exclude the specified package.


10. Enter, Skip, return

In fact, this technique is the most basic knowledge of Debug.
    • F5-step into: Move to the next step, if the current line is a method call, will enter the first line of this method. (Can be excluded by the Nineth article)
    • F6-step over: Moves to the next line. If the current row has a method call, the method is executed and returned to the next line.
    • F7-step Return: Resumes execution of the current method, and when the current method finishes executing, the control will go to the row that the current method is called.
    • F8-moves to the next breakpoint.



oschina.net original translation/ original link

Tips for debugging Java programs 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.