Debug a remote java application with Eclipse

A developper should always be able to start an application in debug mode and run it step by step. While most IDE let you run and debug an application localy in one single click, debugging a remote application needs a few additionnal steps.

These steps are really simple but are usually a blocker for new Java developpers. This is what you need to do in order to run a JVM in debug mode and attach to it from Eclipse IDE :

Just add these options to the JVM :
-Xdebug
-Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000

This can be added to a launch script (Tomcat, custom application, …) or using a administration console (Glassfish, …). It’s often enough to look for the java command in your startup script.

The suspend switch makes the JVM wait for a debug connection before resuming application startup :

  • ‘y’ should be used when you have to catch a breakpoint right at the begining of your application
  • ‘n’ should be used when you want to be able run the application as usual and keep the possibility to connect at anytime. Note: running an application in debug mode is slower, even if you don’t attach to it.
  • Then In Eclipse, open and select your project, and use Debug configuration…

    Debug as...

    Debug as... Debug Configurations...

    Create a new debug configuration for a remote Java application :

    Remote Java Application

    Remote Java Application

    Change host value to your remote application host or keep ‘localhost’ if Eclipse and your application both run on the same machine and press Debug.

    Remote connection

    Remote connection

    Your application should immediately resume (if you used suspend=y) and you’ll be able to create breakpoints and use the debug perspective as usual.

    Note : I know there are tons of posts on this topic all across the internet, but I like to have it here too :-)






Leave a comment

Use your gravatar account to get your photo displayed with your comment.

blog comments powered by Disqus