IDEA remote debugging server java application

Try to ensure that the code used locally should be slightly different from the war/jar package, otherwise you cannot enter the breakpoint

IDEA placement

insert image description here
Set host address and debug port

jar package startup configuration (to ensure consistent ports)

java -jar -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=8089 demo.jar

The war package uses tomcat

If it is windows, set catalina.bat in the tomcat bin directory

rem Configure JAVA 9 specific start-up parameters
set "JDK_JAVA_OPTIONS=%JDK_JAVA_OPTIONS% --add-opens=java.base/java.lang=ALL-UNNAMED"
set "JDK_JAVA_OPTIONS=%JDK_JAVA_OPTIONS% --add-opens=java.base/java.io=ALL-UNNAMED"
set "JDK_JAVA_OPTIONS=%JDK_JAVA_OPTIONS% --add-opens=java.rmi/sun.rmi.transport=ALL-UNNAMED"
rem 添加
set CATALINA_OPTS="-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=8089" 

If it is linux, set catalina.sh in the tomcat bin directory, the settings are as follows

# Add the JAVA 9 specific start-up parameters required by Tomcat
JDK_JAVA_OPTIONS="$JDK_JAVA_OPTIONS --add-opens=java.base/java.lang=ALL-UNNAMED"
JDK_JAVA_OPTIONS="$JDK_JAVA_OPTIONS --add-opens=java.base/java.io=ALL-UNNAMED"
JDK_JAVA_OPTIONS="$JDK_JAVA_OPTIONS --add-opens=java.rmi/sun.rmi.transport=ALL-UNNAMED"
CATALINA_OPTS="-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=8089" #添加
export JDK_JAVA_OPTIONS
export CATALINA_PID=/www/server/tomcatHSOA/apache-tomcat-9.0.21/logss/tomcat.pid #添加

Note that CATALINA_PID must be added, because if it is not added, the open socket port will not be closed normally when the project is closed, and tomcat.pid must be configured with permissions, otherwise it will not be automatically closed. The configuration under windows is similar

illustrate

These parameters are used to configure the Java Virtual Machine (JVM) to support remote debugging. Below is an explanation of each parameter:

-agentlib:jdwp: This is the option to enable the Java debug agent library. jdwp stands for Java Debug Wire Protocol, which is a protocol for communication between Java debuggers and applications.
transport=dt_socket: This is to specify the transport method for communication between the debugger and the application being debugged using sockets. dt_socket means to use socket for transmission.
server=y: This is to specify the JVM to run as a debugging server. This means that the JVM will wait for the debugger's connection request.
suspend=n: This is to specify whether the JVM will suspend and wait for the debugger's connection request when it starts. n means no pause, the JVM will continue to execute normally.
address=8089: This specifies the address and port where the debugger connects. In this example, the debugger will connect to local port 8089.

By using these parameters, you can enable remote debugging on the JVM and configure how and what port the debugger connects to. In this way, you can use IntelliJ IDEA or other tools that support remote debugging to connect to the JVM and perform remote debugging operations.

Precautions

Do not use it in a production environment. After debugging, remember to turn off the added configuration because it is not safe or configure the username and password: as follows but I have not tested it

要设置远程调试的用户名和密码,您可以使用Java的JPDA_ADDRESS环境变量来实现。请按照以下步骤进行操作:

打开Tomcat的启动脚本文件(通常是catalina.sh或catalina.bat)。

在文件的顶部,找到设置Java选项的地方。

在设置Java选项的地方,添加以下行:

export JPDA_ADDRESS=8089
export JPDA_TRANSPORT=dt_socket
export JPDA_SUSPEND=n
export JPDA_OPTS="-Djava.security.debug=all -Djava.security.auth.login.config=$CATALINA_HOME/conf/jaas.config"
创建一个名为jaas.config的文件,并将以下内容添加到文件中:

tomcat {
    
    
    org.apache.catalina.realm.JAASMemoryLoginModule required
    debug=true
    user=test
    password=test;
};
将user和password的值设置为您希望使用的实际用户名和密码。

保存文件并重新启动Tomcat。

现在,您可以使用IntelliJ IDEA或其他调试工具连接到Tomcat的8089端口,并提供正确的用户名和密码进行身份验证。

Guess you like

Origin blog.csdn.net/weixin_43051544/article/details/131749562