java 调试技巧

时间:2019-11-11
本文章向大家介绍java 调试技巧,主要包括java 调试技巧使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

远程调试

1、远程调试是指,开发者可以通过网络连接在远端运行的JVM(例如服务器上运行的应用),并设置断点进行debug。

2、JVM远程调试使用的协议是JDWP(Java Debug Wire Protocol)。

3、使用idea远程调试应用的步骤

1):在远端启动应用时需要在jvm参数中指定开启JDWP功能。 示例: java -jar -Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=5005 debug_xx.jar

其中,server=y表示接收debugger的连接。suspend=y表示等待debugger链接后才开始执行main方法。=n表示相反含义。英文版说明如下(https://mahmoudanouti.wordpress.com/2019/07/07/remote-debugging-java-applications-with-jdwp/):

For example, to launch the JVM with debug options to listen on an address, we use the following option with the java executable:

java -agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=8000  ... MainClass
The -agentlib:jdwp with the comma-separated key-value suboptions instruct the JVM to load the JDWP agent and wait for a socket connection on port 8000. Here’s what each suboption does:

transport=dt_socket tells the JDWP agent to use socket transport.
server=y means that the JVM will listen for a debugger to attach to it.
suspend=y means the JVM will wait for the debugger to attach before executing the main class. This is also the default value. If set to n, the JVM will immediately execute the main class, while listening for the debugger connection.
address=8000 specifies the address at which the debug socket will listen. In this case, the JVM will listen at port 8000 for incoming connections only from the local host (starting JDK 9).
View Code

2):使用idea连接远端JVM并调试。 从idea的Run->Edit Configurations菜单中,新建一个Remote类型的启动配置,启动该配置即可调试应用。配置信息图示如下:

idea中设置条件断点

1、在idea中代码左侧行号处,单击生成一个断点

2、右键点击表示断点的小圆点,弹出debug弹窗,在condition中编写条件表达式。图示如下:

原文地址:https://www.cnblogs.com/afraidToForget/p/11836784.html