Thursday, August 04, 2005

Remote Debugging Java with eclipse

This is a powerful feature which allows you to debug a remote server from an IDE, and step through the code to see what is happening. For example a remote web server like tomcat, or an app server like WebLogic or Websphere can be debugged.

Firstly the basis of this is a feature in the Java JVM. This means that any java executable can be debugged this way.

To start debugging on a java instance you need to include the following when you run the JVM

java -Xdebug -Xnoagent -Xrunjdwp:transport=dt_socket,server=n,suspend=n,address=8000 -Djava.compiler=NONE

For running Weblogic Server in debug mode subtle difference
java -Xdebug -Xnoagent -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=8000 -Djava.compiler=NONE

This sets the debugger listening on a socket on port 8000.

Debugging can also be set up using shared memory, but eclipse can't use the shared memory feature so I won't deal with it here (BTW, Netbeans can use the shared memory mechanism for debugging so this can be pursued if you are using Netbeans)

Simply then within eclipse you need to select Run/Debug.... From there select Remote Debugging, select the project where you have the source code for the project, and specify the server name where the service is running and the port (8000 in our case), and you're laughing.

Easy peasy.

If you are running tomcat (well version 5.5 anyway), the startup scripts already have the debugging information included, see this link

What I've done then is simply changed the startup.bat file.

At the start (line 9) I added the following, which specifies socket based debugging (shared memory is the default), and sets the listen port to 8000
rem ++++++++++++++++++++++++++++++++++++++
rem Enable Remote Debugging
set JPDA_TRANSPORT=dt_socket
rem ++++++++++++++++++++++++++++++++++++++

Careful of any extra spaces of tabs on these line. An extra space caused me some problems for a while.

Then change the 2nd last line where it calls the catalina.bat file from



Now when you call the startup.bat you can simply call startup jpda to start the debugger listening on port 8000. Then you can attach your IDE to port 8000 and debug the server.

Alternatively you can simply call the catalina.bat file directly using the command

catalina.bat jpda start

But you must declare JPDA_ADDRESS=8000 and JPDA_TRANSPORT=dt_socket as environment variable (or put them in the catalina.bat)


Remote debugging with eclipse

To enable remote debugging for weblogic the following must be done

In the setDomainEnv.cmd or you must assign the variable debugFlag to true. (Note no need for quotes etc as I discovered after some time debugging)


set debugFlag=true

local debugFlag=true

No comments: