Explain Codes LogoExplain Codes Logo

What are Java command line options to set to allow JVM to be remotely debugged?

java
debugging
jvm-options
remote-debugging
Alex KataevbyAlex Kataev·Dec 10, 2024
TLDR

Understand the syntax to enable the JVM for remote debugging:

-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=*:8000
  • agentlib:jdwp starts the debugging.
  • transport=dt_socket uses socket connection.
  • server=y enables JVM to accept connections.
  • suspend=n avoids waiting for the debugger (change to suspend=y if you want JVM to wait).
  • address=*:8000 opens port 8000 for any host (customize as per your requirement).

For Java versions earlier than 5.0, use:

-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=1044

Take special note of Java 9+'s tightened security: it binds JDWP by default to localhost. '*:port' syntax enables remote connections.

Decoding debug command options

The JVM debug command line is like a cocktail of options, mix and match as per taste but know what goes in:

  • transport: Specifies the mode of transport. In this case, dt_socket represents a TCP/IP connection.
  • server: Operating mode—Conforms to the principle of "Who waits for whom?" y means the JVM waits, n means the debugger waits. Patience is a virtue after all.
  • suspend: The reins to the JVM startup. y makes the JVM wait until a debugger joins the party.
  • address: The port for the link. 'address=*:port' pattern, where * allows any remote connection.

Troubleshooting and scenarios

An unwanted tenant - Address already in use

netstat -an | grep 8000 #Who's hogging my port!

If another process is hogging your port, change your debug port or evict the pest!

Firewalls - Not just for buildings

Just another day being a network hero—ensure no firewalls become villains between the debug client and server restricting TCP connections over the port.

Protect your realm - Security concerns

Warding off evil and keeping spammers away! Understand the security implications of exposing your JVM to remote debugging.

Mastering debugging nuances

Dockerized applications — debug in a box

EXPOSE 8000 //Set the stage for debugging in the port

Run with equivalent parameters as you would run locally—only in a pretty looking container—like a debug gift box!

Microservices — The debug traffic signal

Maintain cordial relations between your microservices by avoiding debug traffic jams—use a different port for each service.

Debugging on the cloud

Raise your debugging game to the sky! For deploying on cloud, configure security groups or firewall rules to allow your debugging connections.