Thursday, August 19, 2010

Debugging JConsole plugins from Eclipse

I've recently been working on a plugin for JConsole - the Java Management Console that is bundled with Sun JDK 1.5 and 1.6 and IBM Java SDK 1.6. Logging was an OK debugging technique to start with, but as my code grew more and more complex it became more cumbersome to pick through logs. I did a bit of searching and found several forums with folks asking how to do code level debug in Eclipse, but no solution. After I bit of head scratching and experimentation, I found a nifty solution that works great in an Eclipse environment. I am currently building my JConsole plugin project in Eclipse (as a Java project) and have a JAR file specification to generate a plugin jar.

In order to enable (remote) debugging of JConsole execute JConsole with these options:

jconsole -pluginpath /myPlugin.jar -J-Xdebug -J-X noagent -J-Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000

The kicker is specifying each JVM option as a separate -J options. I have an Eclipse external tool configured to start the console with these options, but you can run from the command line as well. The suspend=y option tells the JVM to suspend JConsole startup until a remote debugger connects. After executing the command you'll see something like:

Listening for transport dt_socket at address: 8000

The JConsole window won't actually display until you've connected with the Eclipse remote debugger. To do that, highlight your plugin project and select Run -> Debug Configurations... from the Eclipse menu. Select "Remote Java Application" in the left pane, right click and choose "New". Make sure your project is specified in the connect tab and keep the defaults for connection type and connection properties. If port 8000 is in use on your machine you'll need to specify a different port for the jConsole Java args and as the Eclipse remote connection port. Next, click Debug. The JConsole window should pop up and the debugger should halt on any breakpoints you've set in your plugin code. Happy debugging!

No comments:

Post a Comment