Java程序员应该知道的10个调试技巧

1.不要使用System.out.println作为调试工具

2.把所有涉及到的组件日志级别激活并使用

  • Enable detailed log level of all the components involved.

3.使用日志分析器来读取日志

  • Use a log analyzer to read logs.

0.

Conditional Breakpoint 条件断点(如果你不知道如何添加断点

Hope we know how to add a breakpoint. If not, just click on the left pane (just before the line number) and a breakpoint will be created. In debug perspective, ‘Breakpoints’ view will list the breakpoint created. We can add a boolean condition to it. That is, the breakpoint will be activated and execution will hold only if the boolean condition is met otherwise this breakpoint will be skipped.

如果你不知道如何添加断点,只需点击左边面板(行号前面)断点即被创建。在调试界面中,“断点”视图会把所有被创建的断点列出来。我们可以给它加一个布尔条件,也就是说,该断点会被激活并且如果布尔条件为真,就会执行该断点,否则将会跳过往下执行。

Java程序员应该知道的10个调试技巧

1.

Exception Breakpoint 异常断点

In Breakpoints view there is a button labeled as J! We can use that button to add a java exception based breakpoint. For example we want the program to halt and allow to debug when a NullPointerException is thrown we can add a breakpoint using this.

在断点视图中,有一个J!标记按钮!我们可以使用该按钮来添加一个Java异常断点。例如,我们想让程序在遇到空指针异常(NullPointerException)时,仍然能继续调试,那么我们可以使用该按钮来添加一个异常断点!

Java程序员应该知道的10个调试技巧

2.

Watch Point监视点

This is one nice feature I love. When a chosen attribute is accessed or modified program execution will halt and allow to debug. Select a class variable in Outline view and from its context menu select Toggle Watchpoint. This will create a watch point for that attribute and it will be listed in Breakpoints view.

这是一个非常好的功能,当选定的属性访问或修改程序时,程序会停止执行并允许进行调试。在Outline视图中选择一个类变量并从上下文菜单中选择切换监视点,属性监视点将会被创建,在断点(Breakpoints)视图中会把所有监视点用列表的形式显示出来。

Java程序员应该知道的10个调试技巧

3.

Evaluation (Display or Inspect or Watch)评估/检查

Ctrl+Shift+d or Ctrl+Shift+i on a selected variable or expression will show the value. We can also add a permanent watch on an expression/variable which will be shown in Expressions view when debug is on.

按Ctrl+Shift+D或者Ctrl+Shift+I来显示选定变量或者表达式的值。我们也可以给一个变量或表达式添加永久观察点,当程序在调试时,这些观察点就会在表达式视图(Expression view)中显示出来。

Java程序员应该知道的10个调试技巧

4.

Change Variable Values 修改变量值

We can change the value of a variable on the fly during debug. Choose a variable and go to Variables view and select the value, type and enter.

在调试过程中,我们可以修改变量值。先选好一个变量然后进入变量视图(Variables view),根据变量类型在其对应的Value列里输入值即可。

Java程序员应该知道的10个调试技巧

5.在Main函数里面停止执行

In Run/Debug Settings, Edit Configuration we can enable a check box that says Stop in main. If enabled when we debug a java program that launches with a main method, the execution halts at first line of main method.

运行/调试设置中,编辑配置对话框中有“Main”这个选项卡,我们可以勾选“Stop in main”这个复选框。如果选中,那么在调试一个基于main方法的Java程序时,程序会在main方法第一行位置便停止执行。

Java程序员应该知道的10个调试技巧

6.环境变量

Instead of going to System properties to add an environment variable, we can conveniently add it through Edit Configuration dialog box.

并不是在系统属性中添加环境变量,我们可以在编辑配置对话框中很方便地进行添加。

Java程序员应该知道的10个调试技巧

7.Drop to Frame

This is the second best feature I love. We can just return the control to any frame in the call stack during debug. Changes made to variables will not be reset. Choose the stack level which you want to go back and restart debug from there and click the drop to frame button from debug toolbar. Eclipse is cool!

这也是我最喜欢的一个功能。调试期间,可以重新跳到调用堆栈框架的开始处执行,并且变量值也会回到最初。根据回档调整堆栈的深度,这个功能的主要用途是所有变量状态可以快速回到方法开始执行时候的样子,然后你可以重新进行一遍一遍执行,这样就可以在你关注的地方进行多次调试,但是在执行过程中也会产生一些副作用,比如插入到数据库里面的数据是无法删除的!

Java程序员应该知道的10个调试技巧

8.

Step Filter分布过滤

When we Step Into (F5) a method we may go into external libraries (like java) and we may not need it. We can add a filter in preferences and exclude packages.

当我们进入(F5)方法的时候,我们还可以访问其外部库(比如java.*),我们可能不需要这个库,就可以在Perference选项卡页面添加一个过滤器来排除这个包。

Java程序员应该知道的10个调试技巧

9.

Step Into, Over and Return进入、跳出和返回

I kept this as the last point as this is the first thing to learn in debugging :-)

  • F5 – Step Into: moves to next step and if the current line has a method call the control will go into the first line of the called method.
  • F6 – Step Over: moves the control to next line. If there is a method call in the current line, it executes the method call internally and just moves the control to next line.
  • F7 – Step Return: When done from inside a method the control will move to the calling line from where the current method is invoked.
  • F8 – Move to next breakpoint.

我把这个放在最后一点,在调试过程中,这些是必须要了解(最好掌握)的东西:

F5——进入:移动到下一个步骤,如果当前行有一个方法调用,该控件将会跳转到被调用方法的第一行执行。

F6——跳出:移动到下一行。如果在当前行有方法调用,那么会直接移动到下一行执行。不会进入被调用方法体里面。

F7——返回:从当前方法中跳出,继续往下执行。

F8——移动到下一个断点处执行

Java程序员应该知道的10个调试技巧

原文地址:https://www.cnblogs.com/assassin/p/3535989.html