溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Java項目開啟遠程調(diào)試的方法步驟

發(fā)布時間:2021-09-14 21:50:54 來源:億速云 閱讀:117 作者:chen 欄目:編程語言

這篇文章主要介紹“Java項目開啟遠程調(diào)試的方法步驟”,在日常操作中,相信很多人在Java項目開啟遠程調(diào)試的方法步驟問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”Java項目開啟遠程調(diào)試的方法步驟”的疑惑有所幫助!接下來,請跟著小編一起來學(xué)習(xí)吧!

環(huán)境

apache-tomcat-8.5.16

Linux

如何啟用遠程調(diào)試

tomcat開啟遠程調(diào)試

方法

切換到你的tomcat的bin目錄/apache-tomcat-8.5.16/bin 下,執(zhí)行:

./catalina.sh jpda start

執(zhí)行上面的命令就可以開啟遠程debug了,如果想配置一些信息,比如端口號什么的,請參考下面的說明。

參數(shù)說明

#  JPDA_TRANSPORT (Optional) JPDA transport used when the "jpda start"#          command is executed. The default is "dt_socket".##  JPDA_ADDRESS  (Optional) Java runtime options used when the "jpda start"#          command is executed. The default is localhost:8000.##  JPDA_SUSPEND  (Optional) Java runtime options used when the "jpda start"#          command is executed. Specifies whether JVM should suspend#          execution immediately after startup. Default is "n".##  JPDA_OPTS    (Optional) Java runtime options used when the "jpda start"#          command is executed. If used, JPDA_TRANSPORT, JPDA_ADDRESS,#          and JPDA_SUSPEND are ignored. Thus, all required jpda#          options MUST be specified. The default is:##          -agentlib:jdwp=transport=$JPDA_TRANSPORT,#            address=$JPDA_ADDRESS,server=y,suspend=$JPDA_SUSPEND

操作說明

所以如果想修改配置,則如下操作:

在catalina.sh中進行配置:

JPDA_TRANSPORT=dt_socket JPDA_ADDRESS=5005 JPAD_SUSPEND=n

或者通過JPDA_OPTS進行配置:

JPDA_OPTS='-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5005'

springboot開啟遠程調(diào)試

遠程調(diào)試maven設(shè)置

The run goal forks a process for the boot application. It is possible to specify jvm arguments to that forked process. The following configuration suspend the process until a debugger has joined on port 5005

<project> ... <build>  ...  <plugins>   ...   <plugin>    <groupId>org.springframework.boot</groupId>    <artifactId>spring-boot-maven-plugin</artifactId>    <version>1.1.12.RELEASE</version>    <configuration>     <jvmArguments>      -Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=5005     </jvmArguments>    </configuration>    ...   </plugin>   ...  </plugins>  ... </build> ...</project>

These arguments can be specified on the command line as well, make sure to wrap that properly, that is:

mvn spring-boot:run -Drun.jvmArguments="-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=5005"

jar 命令開啟遠程調(diào)試

在執(zhí)行jar的時候,添加上參數(shù)。如下:

java -Xdebug -Xrunjdwp:server=y,transport=dt_socket,address=8000,suspend=n -jar demo.jar

如果想深入了解Java調(diào)試,那么去看一下這個吧。深入Java調(diào)試體系

問題

如果出現(xiàn)Connection refused

首先檢查一下端口8000的使用情況:

use:~/tomcat/logs # netstat -an|grep 8000cp    0   0 127.0.0.1:8000     0.0.0.0:*        LISTEN

可見當(dāng)前16808端口服務(wù)被綁定了回環(huán)地址,外部無法訪問。即本地調(diào)試。

辦法:

修改catalina.sh中一個參數(shù)。

if [ -z "$JPDA_TRANSPORT" ]; then  JPDA_TRANSPORT="dt_socket" fi if [ -z "$JPDA_ADDRESS" ]; then  JPDA_ADDRESS="0.0.0.0:8000" fi if [ -z "$JPDA_SUSPEND" ]; then  JPDA_SUSPEND="n" fi

JPDA_ADDRESS="localhost:8000" 把默認值(localhost:8000)改成0.0.0.0:8000。默認是本地ip調(diào)試也就是無法遠程調(diào)試,0.0.0.0表示所有ip地址都能調(diào)試。

遠程連上后再看端口情況:

root@VM-198-217-ubuntu:/opt/apache-tomcat-8.5.16/bin# netstat -an | grep 8000tcp    0   0 10.133.198.217:8000   60.177.99.27:49998   ESTABLISHED

斷開后是這樣的:

root@VM-198-217-ubuntu:/opt/apache-tomcat-8.5.16/bin# netstat -an | grep 8000tcp    0   0 0.0.0.0:8000      0.0.0.0:*        LISTENtcp    0   0 10.133.198.217:8000   60.177.99.27:49998   TIME_WAIT

idea連接遠程端口進行遠程debug

我已經(jīng)在服務(wù)器上開啟了遠程調(diào)試,idea連接的步驟,直接上圖。

edit configurations

遠程調(diào)試配置

參數(shù)配置

將紅框內(nèi)的地址和端口號改成自己的。

啟動遠程調(diào)試

成功界面

請求一下試試

調(diào)試的姿勢和本地調(diào)試一樣,開始造起來吧!

到此,關(guān)于“Java項目開啟遠程調(diào)試的方法步驟”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識,請繼續(xù)關(guān)注億速云網(wǎng)站,小編會繼續(xù)努力為大家?guī)砀鄬嵱玫奈恼拢?/p>

向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI