Skip to content

Instantly share code, notes, and snippets.

@rsrini7
Last active September 18, 2022 00:48
Show Gist options
  • Save rsrini7/8a1182c7a848e48488fdb1865e6e273b to your computer and use it in GitHub Desktop.
Save rsrini7/8a1182c7a848e48488fdb1865e6e273b to your computer and use it in GitHub Desktop.
Remote Debugging
We use a Socket Attaching Connector, which is enabled by default when the dt_socket transport is configured and the VM is running in the server debugging mode.
#Java Env Variable (Append $JAVA_OPTS or %JAVA_OPTS% if required)
JAVA_OPTS="-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=*:5005"
#For Java 9 or above
Since Java 9.0 JDWP supports only local connections by default, add*:<port> to listen all interfaces
java -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=*:5005 <AppJar>
#For java 1.5 to 1.8:
java -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5005 <AppJar>
#For java 1.4:
#Before Java 5.0, use -Xdebug and -Xrunjdwp arguments. These options will still work in later versions, but it will run in interpreted mode instead of JIT, which will be slower.
java -Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=5005 <AppJar>
#For java 1.3:
java -Xnoagent -Djava.compiler=NONE -Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=5005 <AppJar>
#For Spring Maven
mvn spring-boot:run -Drun.jvmArguments=**"-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5005"
# Spring Gradle
gradle bootrun --debug-jvm
# Docker
docker run -it -e JAVA_OPTS='-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=*:5005' -p 5005:5005 <image>
Options on -Xrunjdwp or agentlib:jdwp arguments are :
transport=dt_socket : means the way used to connect to JVM (socket is a good choice, it can be used to debug a distant computer)
address=5005 : TCP/IP port exposed, to connect from the debugger,
suspend=y : if 'y', tell the JVM to wait until debugger is attached to begin execution, otherwise (if 'n'), starts execution right away.
It is not required to add the address parameter. If not provided the agent is selecting a random port number. This might be useful if you start multiple nodes within the same java command line
@rsrini7
Copy link
Author

rsrini7 commented Sep 18, 2022

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment