麦克斯仇
Think different
159
文章
27970
阅读
首页
INDEX
文章
ARTICLE
关于
ABOUT
利用JDWP协议对远程Java项目或远程Tomcat进行debug
创建日期:
2020/03/28
修改日期:
2023/10/24
Java
Tomcat
# 介绍 IBM上的介绍:[https://www.ibm.com/developerworks/cn/java/j-lo-jpda3/index.html](https://www.ibm.com/developerworks/cn/java/j-lo-jpda3/index.html) 官方教程:[https://docs.oracle.com/javase/8/docs/technotes/guides/troubleshoot/introclientissues005.html](https://docs.oracle.com/javase/8/docs/technotes/guides/troubleshoot/introclientissues005.html) # 实战 ### debug普通jar项目 #### demo准备 使用Springboot搭建demo `pom.xml`如下 ```xml <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.2.5.RELEASE</version> <relativePath></relativePath> <!-- lookup parent from repository --> </parent> <groupId>com.max</groupId> <artifactId>demo</artifactId> <version>0.0.1-SNAPSHOT</version> <name>demo</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project> ``` `Application`如下 ```java package com.max.demo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @SpringBootApplication @RestController public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } @RequestMapping("/") public String demo() { StringBuilder str = new StringBuilder(); for (int i = 0; i < 10; i++) { str.append(i); } return str.toString(); } } ``` 执行`maven`打包后,上传jar包至服务器 #### 启用监控 根据jdk版本不同,添加的参数也不同 ```bash # jdk9或者更新 -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=*:8000 # jdk5-jdk8 -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=8000 # jdk1.4.x(jdk8也能用) -Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=8000 ``` > `suspend=n`代表不暂停,立即启动项目,如果设置为`suspend=y`,则启动后需要客户端连接后才项目才会启动 启动监控示例如下 ```bash java -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=8000 -jar demo-0.0.1-SNAPSHOT.jar ``` #### 使用开发工具连接并开始调试 ##### IntelliJ IDEA 1.在示例的`StringBuilder str = new StringBuilder();`设置断点 ![](https://cdn2.maxqiu.com/upload/0d7783450975493db9ed6f03376b3f45.jpg) 2.点击菜单栏`run` -- `Edit Configurations`;或者右上角下拉后点击`Edit Configurations` ![](https://cdn2.maxqiu.com/upload/974db1984763457d9f05cf2c04f9ddeb.jpg) ![](https://cdn2.maxqiu.com/upload/b7bf4318369846aa8697c846683eda22.jpg) 3.点击`+` -- `Remote` ![](https://cdn2.maxqiu.com/upload/2cb6f5f00db2460bbd97be81b720a738.jpg) 4.`Host`填写服务器IP,`Port`填写远端启动命令中`address`设置的端口,点击`Ok`保存 ![](https://cdn2.maxqiu.com/upload/3192c8ae32b6421c884b716d9367ab04.jpg) 5.点击右上角的`Debug`,控制台提示连接`Connected to ...` ![](https://cdn2.maxqiu.com/upload/343cc810fb6046cd8b53af9418e79a0c.jpg) 6.浏览器访问项目,可以看到IDEA进入debug模式 ##### Eclipse 1.在示例的`StringBuilder str = new StringBuilder();`设置断点 ![](https://cdn2.maxqiu.com/upload/81e03cb3374b44acadd01adc1e353ec0.jpg) 2.点击菜单栏`run` -- `Debug Configurations` ![](https://cdn2.maxqiu.com/upload/9035d3433cf746a69f8c897c707c7a96.jpg) 3.右击`Remote Java Application`,点击`New Configuration` ![](https://cdn2.maxqiu.com/upload/2d45dd19e2b04c1087bcc1053981a178.jpg) 4.`Host`填写服务器IP,`Port`填写远端启动命令中`address`设置的端口,点击`Apply`保存,点击`Debug`开始调试 ![](https://cdn2.maxqiu.com/upload/1f698eb22ce24043a326de2367eb28f3.jpg) > 点击`Debug`后,如果正常连接,控制台不会有输出,也没找到关闭调试的地方(可能是我的软件问题),所以还是推荐使用IDEA 5.浏览器访问项目,可以看到Eclipse进入debug模式 ### Tomcat开启远程调试 编辑`startup.sh`,在最后一行的`start`之前添加`jpda` ```bash exec "$PRGDIR"/"$EXECUTABLE" jpda start "$@" ``` 编辑`catalina.sh`,在339行(一般情况下),修改`JPDA_ADDRESS`允许远程连接 ```bash if [ -z "$JPDA_ADDRESS" ]; then JPDA_ADDRESS="8000" fi ``` 重启Tomcat后,查看Tomcat进程,可以看到有jdwp的参数 开发工具连接步骤同上
94
全部评论