The JVM provides a useful feature called the Java Platform Debugger, or JPDA. When enabled via startup options, it provides a port through which debuggers (e.g. Eclipse) are able to connect and do their thing. This allows you to debug practically any application, such as Tomcat, or in my case, WebTest (running inside of an Ant process).
There are a bunch of available options for JPDA, but here is the basic incantation:
- jdwp: Java Debug Wire Protocol
- transport: indicates that debuggers should attach using sockets, as opposed to the other option, which is shared memory (dt_shmem)
- server: when 'y', listens for a debugger application to attach (otherwise, attempt to connect to the specified debugging instance)
- suspend: the JVM should wait for a debugger application to attach before starting up
- address: the port to expose for debugger applications (or address to connect to in order to debug)
*Note: you may have seen a different set of arguments for starting a JVM in debug mode:
This is required for Java versions < 5.
You can find a full list of options here.
Now that you have your application faithfully waiting for you to debug it ('Listening for transport dt_socket at address: 8008'), you just have to set up a remote debug configuration in Eclipse and run it:
- Run > Debug Configurations... > new Remote Java Application
- Connection Type: Standard (Socket Attach)
- Connection Properties: host and port of JDWP application
- Allow termination of remote VM: if selected, when you are finished with the debugger and click the terminate button in Eclipse, the remote application will exit also