Enable JDWP
If -agentlib:jdwp is passed on starting, JDWP(Java Debug Wire Protocol), which is used for communicating between debugger and JVM, is enabled.
- transport=dt_socket,server=y,address=*:5005: Listen debugger on port 5005. Prior to Java 8, *: is not required.
- suspend=n: Don’t suspend the JVM immediately before the main class is loaded
For applications that terminate when processing is finished, you can start the debugger in advance and then run the application with server=n to connect to the debugger. Or with suspend=y, you can get the application paused until a debugger connects to it.
$ java --version
openjdk 18.0.1 2022-04-19
OpenJDK Runtime Environment Corretto-18.0.1.10.1 (build 18.0.1+10-FR)
OpenJDK 64-Bit Server VM Corretto-18.0.1.10.1 (build 18.0.1+10-FR, mixed mode, sharing)
$ java -jar -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=*:5005 out/artifacts/debugtest_jar/debugtest.jar
Listening for transport dt_socket at address: 5005
Start server on :8080
Port forwarding
Enable the debugger to connect the JVM in the remote machine by port forwarding with SSH or AWS SSM etc.
$ ssh [email protected] -Nf -L 5005:localhost:5005
$ aws ssm start-session --target instance_name --document-name AWS-StartPortForwardingSession --parameters '{"portNumber":["5005"], "localPortNumber":["5005"]}'
Options for SSH port forwarding - sambaiz-net
Run
For IntelliJ, add Remote JVM Debug from “Run > Edit Configurations”.
Connected to the target VM, address: 'localhost:5005', transport: 'socket'