10 tips for debugging Java programs with eclipse

Source: Internet
Author: User

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 know or have forgotten 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 about good code design.

-Breakpoint View: Conditional breakpoint

This is useful if you are interested in only one 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.

  

  

-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 not always 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 embarrassing for a developer like me.

"Show Logical Structure" is not turned on

  

Open "Show Logical Structure"

  

-Variable View: Change the value of a variable ...

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).

  

-Navigation: Drop to Frame

I think anyone 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 return in time

You can directly fallback 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".

  

-Navigator: Using the keyboard

If you avoid using a mouse, you will be quicker to operate. You should at least have the following shortcut keys:

f5– "one Step Into"

f6– "stepping and skipping"

f7– "Stepping and returning"

f8– "Continue execution"

ctrl+shift+b– "Add Breakpoint"

ctrl+shift+i– "Check"

-Breakpoint View: watchpoints

What happens if we change this variable?! Sometimes it is very useful to create a watchpoint 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.

  

-Friendly and readable objects

A variable view is a value that uses the ToString method of the object to realistically correspond. 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 that represents the object. Typically, the ToString method returns a string that "literally" represents 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 the Tostringbuilder in Commons-lang. It provides functionality to write a "good and consistent" ToString method (referenced from Javadoc).

The default ToString

  

The default Tostringbuilder example

  

Tostringbuilder Example – Multi-line 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.

High-end photo book http://www.biyinjishi.com/products/a65-b6530/d100132/
Photo albums http://www.biyinjishi.com/products/a65-b6530/d100133/
Wood Print http://www.biyinjishi.com/products/a65-b6535/d100135/
Crystal Print http://www.biyinjishi.com/products/a65-b6535/d100136/
Photo Frames http://www.biyinjishi.com/products/a65-b6535/d100137/
Creative Home http://www.biyinjishi.com/products/a65-b6550/d100137
Cup http://www.biyinjishi.com/products/a65-b6550/d100139/
t -shirts http://www.biyinjishi.com/products/a65-b6550/d100140/
Sweatshirt http://www.biyinjishi.com/products/a65-b6550/d100140/
Notepad http://www.biyinjishi.com/products/a65-b6550/d100141/
Mobile Peripheral http://www.biyinjishi.com/products/a65-b6550/d100142/
Pillow http://www.biyinjishi.com/products/a65-b6550/d100143/
Invitation http://www.biyinjishi.com/products/a65-b6550/d100144/
Greeting Card http://www.biyinjishi.com/products/a65-b6550/d100144/
Anthology http://www.biyinjishi.com/products/a65-b6550/d100148/
Poetry http://www.biyinjishi.com/products/a65-b6550/d100148/

10 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.