How to suspend VM on startup when remote debugging your Java app

You want to do a remote debugging session on code that runs in the initial few seconds of your app being launched.

A problem that you can run into when attempting to do this is that since the code runs right in the beginning, the JVM may rush past it before you have had a chance to attach a debugger via Eclipse and set your break points.

The solution is to setup your JVM/debugging config so that it is set to suspend at startup.

A typical way to setup remote debugging is to use the following JVM argument in the code used to launch your application so that a debugger can later attach itself to port 8000.

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

Infact this is what Eclipse does when you launch an app in debug mode rather than normal mode. It adds this line in the JVM arguments used when launching your application.

Note the suspend=n. If that is set to y, the JVM will suspend on startup. It will wait for a debugger to attach to it and release it before it will proceed.

So using the suspend flag, our problem is solved. Happy debugging!



Tags:
This entry was posted on Monday, May 23rd, 2011 at 7:31 pm and is filed under Uncategorized. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

3 Responses to “How to suspend VM on startup when remote debugging your Java app”

  1. Payal

    Thanks for providing such a nice information.It helped me while I was doing remote debugging.

  2. Tomcat suddenly throwing a ClassFormatError on startup | PHP Developer Resource

    [...] this is to use eclipse’s break on exception feature. Start tomcat so that it waits for a debugger to attach before running, then break on the java.lang.ClassFormatError being thrown, and look around the local variables and [...]

  3. www.essexcaraudioandalarms.co.uk

    When some one searches for his essential thing,
    thus he/she needs to be available that in detail, thus that thing is maintained over here.

Leave a Reply

Your comment