Tuesday, May 5, 2009

Configure Eclipse for Remote Debugging

A J2EE developer always finds it difficult to debug a web application. The developer has to rely on log4j logging to understand what is happening. Although exception stack trace provides valuable info but at times it's not enough to debug a problem. Even for a trivial problem sometimes debugging takes ages. What if a developer can follow the execution in real-time and able to see the flow, values assigned to the variables, etc. Well a Java debugger is there to make debugging comfortable and quick. My favorite Eclipse IDE provides the remote debugging with lots of useful features.

But first we need to know how to enable a JVM to be debugged remotely. Any application server like JBoss, Tomcat, Weblogic, etc. or a simple JVM instance can be debugged remotely. Pass the following arguments to a JVM to enable remote debugging.

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

If both the debugger and target JVM is 5.0 and above then the preferred way is given below, although the above will also work.


More information of Java debugger is available here

To enable Jboss or Tomcat for remote debugging, you need to set JAVA_OPTS environment variable with the above arguments. Start the server, the Java Debugger is ready to listen on the defined port.

Follow the below steps to configure Eclipse for debugging -

  • In Eclipse go to Run > Debug Configurations..., a popup will open.

  • Right click on "Remote Java Application" and select New.

  • Give some name so that you can identify this debugger.

  • Select a project which you want to debug.

  • Select "Standard (Socket Attach)" as Connection Type.

  • Provide the host and port details where the remote JVM is running. For the above example port number will be 8787.

  • Click on Debug and Eclipse will open the Debug perspective which provides lots of helpful features.

  • The first task to debug a problem is to set breakpoints in the code where you think the problem persists. Eclipse allows conditional breakpoints which gets triggered when the defined condition evaluates to true. At runtime value of a variable can be changed to debug the code. Another useful functionality is to add a Java exception breakpoint, which gets triggered when the said exception is thrown.

    No comments:

    Post a Comment