写在前面
工作中,有时会出现测试环境有问题,本地却正常的情况,此时我们就可以通过JVM提供的远程调用的功能,实现在本地debug调试测试环境代码。
1:例子1直接运行class
首先我们来定义类:
package com.feng.map.demo;
import java.util.*;
public class MapMain {
public static void main(String[] args) {
Map<String, String> map = new HashMap<>();
map.put("aa", "aa");
map.put("bb", "bb");
map.put("cc", "cc");
map.put("dd", "dd");
map.put("ff", "f");
Set<Map.Entry<String, String>> entrySet = map.entrySet();
for (Map.Entry<String, String> entry : entrySet) {
System.out.println(entry.getKey() + " ------ " + entry.getValue());
}
}
}
接着使用如下命令启动,开启JVM远程debug接口:
D:\test>javac -d . MapMain.java
D:\test>java -agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=9093 com.feng.map.demo.MapMain
Listening for transport dt_socket at address: 9093
这样当前启动的JVM虚拟机就会通过9093端口来监听服务调用了,接着我们配置idea的remote debug功能,如下:
然后我们运行上图配置的remotemap
,就可以进行调试远端代码了,如下:
2:运行jar
自己准备一个可以执行的jar包,我这里是一个基于springboot的项目,如下方式启动:
D:\test>D:\\program_files\\jdk-17.0.1\\bin\\java -jar -agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=9093 pricing-0.0.1-SNAPSHOT.jar
Listening for transport dt_socket at address: 9093
11:25:40,036 |-INFO in ch.qos.logback.classic.LoggerContext[default] - This is logback-classic version 1.4.5
11:25:40,059 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Could NOT find resource [logback-test.xml]
11:25:40,061 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Could NOT find resource [logback.xml]
11:25:40,063 |-INFO in ch.qos.logback.classic.BasicConfigurator@1165b38 - Setting up default configuration.
11:25:40,694 |-INFO in ch.qos.logback.core.joran.spi.ConfigurationWatchList@4c12331b - URL [jar:file:/D:/test/pricing-0.0.1-SNAPSHOT.jar!/BOOT-INF/classes!/logback-boot.xml] is not of type file
...
然后在idea中配置远程关联到本地源码,从而在本地debug,如下:
然后我们在想要debug的位置打上断点,如下:
接着访问远端服务器接口,就可以开始debug本地的代码了,如下:
3:部署到Tomcat
参考这篇文章 准备测试项目,之后先使用idea启动,之后会在CATALINA_BASE中生成项目对应的war的解压包,接下来我们测试如下命令是否能够启动注意你本地目录和我不一样
:
set "CATALINA_BASE=C:\Users\Administrator\AppData\Local\JetBrains\IntelliJIdea2020.2\tomcat\Unnamed_test-remote-debug_2" && set "CATALINA_HOME=D:\program_files\apache-tomcat-8.5.66-windows-x64_tmp\apache-tomcat-8.5.66" && set "CATALINA_TMPDIR=D:\program_files\apache-tomcat-8.5.66-windows-x64_tmp\apache-tomcat-8.5.66\temp" && set "JRE_HOME=D:\program_files\jdk1.8.0_202" && set "CLASSPATH=D:\program_files\apache-tomcat-8.5.66-windows-x64_tmp\apache-tomcat-8.5.66\bin\bootstrap.jar;D:\program_files\apache-tomcat-8.5.66-windows-x64_tmp\apache-tomcat-8.5.66\bin\tomcat-juli.jar" && D:\\program_files\\apache-tomcat-8.5.66-windows-x64_tmp\\apache-tomcat-8.5.66\\bin\\catalina.bat run
这样就说明我们配置成功了,之后我们需要配置Tomcat启动的时候启用远程debug的能力,打开catalina.bat
搜索if not ""%1"" == ""run"" goto mainEntry
在其上增加如下内容:
SET CATALINA_OPTS=-server -Xdebug -Xnoagent -Djava.compiler=NONE -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=8000
接着使用上述命令启动,输出如下内容则说明配置成功:
C:\Windows\System32>set "CATALINA_BASE=C:\Users\Administrator\AppData\Local\JetBrains\IntelliJIdea2020.2\tomcat\Unnamed_test-remote-debug_2" && set "CATALINA_HOME=D:\program_files\apache-tomcat-8.5.66-windows-x64_tmp\apache-tomcat-8.5.66" && set "CATALINA_TMPDIR=D:\program_files\apache-tomcat-8.5.66-windows-x64_tmp\apache-tomcat-8.5.66\temp" && set "JRE_HOME=D:\program_files\jdk1.8.0_202" && set "CLASSPATH=D:\program_files\apache-tomcat-8.5.66-windows-x64_tmp\apache-tomcat-8.5.66\bin\bootstrap.jar;D:\program_files\apache-tomcat-8.5.66-windows-x64_tmp\apache-tomcat-8.5.66\bin\tomcat-juli.jar" && D:\\program_files\\apache-tomcat-8.5.66-windows-x64_tmp\\apache-tomcat-8.5.66\\bin\\catalina.bat run
...
Using CATALINA_OPTS: "-server -Xdebug -Xnoagent -Djava.compiler=NONE -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=8000"
Listening for transport dt_socket at address: 8000
05-May-2023 13:56:25.016 信息 [main] org.apache.catalina.startup.VersionLoggerListener.log Server.服务器版本: Apache Tomcat/8.5.66
...
然后配置JVM remote,如下:
写在后面
参考文章列表
tomcat intellij远程调试 。
JVM远程调试功能开启,与原理 。
springmvc之编程步骤 。