Tips for debugging Java programs using Eclipse "9"

Source: Internet
Author: User

If there are any shortcomings, please understand and welcome the criticism, not very grateful.
Please respect the author's labor results:

This article original pipi-changing
Original source of this article: http://www.cnblogs.com/pipi-changing/

This document is owned by the author and the blog Park and must be retained without the author's consent.
And in the article page obvious location to the original link, otherwise reserves the right to pursue legal responsibility.

tips for debugging Java programs with Eclipse

Do not debug too much

  Debug only the code you think is problematic or part of the functionality;

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 process during the 13th cycle

Or debug some functionality in an abstract parent class, and you are only interested in one of the specific implementations. You can set conditions in the Breakpoints view,

or set by the right-click menu ("Breakpoint Properties") labeled by the Blue breakpoint next to the code. You can do this in the conditional code snippet for

True to suspend the program, or suspend the program when the value of the code fragment changes.


As the name implies is a breakpoint with a certain condition, only satisfies the user set conditions, the code will be run to the breakpoint at the stop.

Right-click at the breakpoint and select the last "Breakpoint Properties"

The property interface of the breakpoint and the meaning of each option,

" 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. Assume that you are using the

HashMap, you need to click through each entity entry and face various HASHMAP implementation details. However, on the variable view, there is a call "Show Logical

Structure"button. It is very useful, especially if the information displayed by the ToString () method of your object is not friendly.

"Show Logical Structure" is not turned on

Open "Show Logical Structure"

"Variable view: Change variable Value"

    When you need to change the input information slightly, do not need to restart the debugging session, just enter the new information in a table, you can directly modify the debugging phase you

The value of the variable. There are times when you can save some time and you can simulate some weird situations more simply by this feature.



"Display View "

do you know the "Display view"? You can activate it through "window", "Show View", "Display" during debugging. Now, your eclipse

Should be a blank new 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

This 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 want to mention two I'm very

Prefer the advanced method of navigation. The first one is "Drop to Frame". With this feature you can rewind in time, and you can go back directly to a running Java stack frame

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 easily re-ship

Line a frame code.

"Navigation: Step into Selection"

The second one is "Step into Selection". 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. Phase

This is more convenient than "step into", think of you like tracking into a function with a lot of parameters, if you use "Step into" you must enter, exit each parameter calculation process, only

Can get into the function you really want to go into. "Run To Line" is also a great feature. Just put the cursor in front of which line you want to run, and then press "Ctrl+r" to

The

" Navigator: Using the keyboard "

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 the called method body

Inside.

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

f8--moves to the next breakpoint to execute.

Global single-Step jump selection Ctrl+f5

Global Debug Last started F11

Global use filters to step SHIFT+F5

Global Add/Remove breakpoints Ctrl+shift+b

Global Display Ctrl+d

Global Run last boot ctrl+f11

Global Run to row ctrl+r

Global Execution Ctrl+u

    • ctrl+shift+b– "Add Breakpoint"
    • ctrl+shift+i– "Check"

"Breakpoint View: Watchpoints"

What happens if you change this variable? Sometimes creating a watchpoint is useful for debugging. The debugger will stop, regardless of the

Whether the field being observed is modified or read-You can configure the decision yourself. You just double-click on a field and then you can view the breakpoint in

Watchpoint, and you can edit its properties. You can even set a number of accesses, which means the debugger stops when the variable reaches the number of accesses . This is also true for normal breakpoints.

"Friendly and readable object"

The variable view is used to implement the corresponding value using the object's ToString method. For this reason, if a friendly ToString method is implemented,

Can be very useful for debugging purposes. The default ToString implementation of Java.lang.Object in Javadoc is also recommended:

"Returns a String that represents 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 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 the framework or an external API, you can have another option in

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.

"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. Such as

When you debug a Java program based on the main method, the program stops executing at the first line of the Main method.

"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

To exclude this package by adding a filter to the Perference tab page.

"remote Debugging"

For debugging programs that are not on this computer, there are two ways to

1, the machine as a client

2, the machine as a service side

The premise of using remote debugging is that the server side and client code are consistent.

This machine acts as a client

This machine is commonly used by the client, the Java program on the remote server needs to open the remote debug switch at startup,

Server-side need to add virtual machine parameters

1.5 Previous versions (1.5 also available later):

"-xdebug-xrunjdwp:transport=dt_socket,server=y,address=8000"

Version 1.5 and above: "-agentlib:jdwp=transport=dt_socket,server=y,address=8000"

F:\study\eclipsepro\screensnap>java-xdebug-xrunjdwp:transport=dt_socket,server=y,address=8000-jar Screensnap3.jar

When connecting to a remote server, you need to create a new remote debugger in eclipse

There is a small place to note that the connection on the time does not seem to automatically switch to the debug view, do not think that the local debug program is not connected to the server side.

This machine serves as the service side

You only need to modify the "Connection Type" as a client compared to the native machine.

at this point eclipse will enter the state of waiting for the connection

The connection program uses the following parameters to connect to the local server, IP address should be replaced by implementing IP ~ ~

"-agentlib:jdwp=transport=dt_socket,suspend=y,address=127.0.0.1:8000"

F:\study\eclipsepro\screensnap>java-agentlib:jdwp=transport=dt_socket,suspend=y,address=127.0.0.1:8000-jar Screensnap3.jar

While remote debugging, local code modifications can be synchronized to remote, but will not be written to remote files, that is, local modifications will be launched the next time the remote program

Without affecting the remote code at the next use.

Tips for debugging Java programs using Eclipse "9"

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.