Debugging external Java processes with Eclipse (GWT example)

Many modern IDEs have all sorts of support for running applications, even servers, in process with the IDE; and then debugging from there. It's good to also be aware, however, that you can debug *external* processes pretty easily as well. This is made possible by the Java Platform Debugger Architecture.

bug

Basically, modern JVMs have a set of profiling interfaces (one for the front end, and one for the back end), and a protocol, that work together to enable external debugging.

The JVM has a native interface implementation, based on JVMTI, on the back end, and the front end debugging application uses a Java interface based on JDI. The protocol that enables communication between the two is JDWP. Using this setup debuggers can easily connect to remote Java processes, and perform their typical beloved duties.

To demonstrate the concepts, let's step through an example using GWT-Maven to launch a GWT project, and then debug that project from Eclipse.

To begin, an external Java process needs to be running, and that process must have been configured to pass the -Xdebug and -Xrunjdwp options to the JVM at startup (optionally the newer-agentlib:jdwp option is preferable on Java 5.0 and above VMs).

The exact options used for this example, which are passed automatically via GWT-Maven using the "gwt:debug" goal, are as follows:

-Xdebug -Xnoagent -Djava.compiler=NONE -Xrunjdwp:transport=dt_socket,server=y,address=3408,suspend=y

More detail about all of the options is available in the JPDA documentation.

Basically, in this case, I am telling the GWTShell to start and wait for a debugger to connect before continuing. In a shell session this looks like the following (notice the process stopped, and is waiting for a connection on port 3408):

command window

Once the process is waiting for a debugger to connect, the stage is set. The next thing to do is simply to connect with a debugger to the port shown - this is where Eclipse comes in.

To connect with Eclipse you must use the "Run dialog," and select new "Remote Java Application." From there simply specify a name for the project, and the port as shown (this screen shot shows port 1941 from a previous run, in this case it would need to be changed to 3408 to connect to the GWTShell process started above):

debug dialog

With that, all the pieces are in place, the JVM is running as the back end, and the IDE has connected as the front end. In this example the GWTShell continues along and launches the application, and from there you can click around to exercise the Java code.

As with any typical debugging you can use breakpoints and step into and out of the code, and inspect the state as you go. Though the screen shot here is small, you get the idea with the blue breakpoint dot and green highlighted code line:

debug breakpiont

All in all the process is very easy once you understand the roles of the components. Though this example used GWT, and GWT-Maven (something where external debugging comes in ultra handy), keep in mind that these concepts can be applied to any external Java process (a Tomcat server, a JBoss server, a Swing app, even an Applet - with a few caveats).

Comments

Good morning, First of all,

Good morning,

First of all, thank you very much for this tutorial, it's very interesting, but what I want know, is where did your write this line :
-Xdebug -Xnoagent -Djava.compiler=NONE -Xrunjdwp:transport=dt_socket,server=y,address=3408,suspend=y

Thank you in advance.

Ziad,

http://java.sun.com/javase/6/

http://java.sun.com/javase/6/docs/technotes/tools/windows/java.html

Those are all passed to the java command itself (java.exe or java, depending on your platform, etc). The -X are "non standard options" and the -D are "system properties." So however you launch Java, be it with your IDE, on the command line, or some other way, these options and properties need to be passed.

In the example here these were passed FOR ME by GWT-Maven (which does this if debugging is enabled). You can do the same though with any Java process, from Eclipse itself, if you have something you can "run" you can alter the launch configuration tab to pass such options and properties, etc.

Hello colline! Thank you for

Hello colline!

Thank you for your answer.

You said "In the example here these were passed FOR ME by GWT-Maven (which does this if debugging is enabled)". Iwant to do the same think, but I don't know how to specify this parameters.

thank you so much.

Best regards,

Ziad.

This article was really about

This article was really about debugging external Java processes from Eclipse, I just happened to use GWT as my example because that was really easy when using GWT-Maven.

If you want to do the same you will need to check out GWT-Maven: http://code.google.com/p/gwt-maven/

GWT maven invokes the GWTShell from the command line for you, and if you have debugging enabled (it's a configuration property in the Maven POM) then it adds the parameters this article shows to that command line. If you have other questions about GWT-Maven (after reviewing the docs and samples) then you might want to post them at the GWT-Maven thread: http://groups.google.com/group/gwt-maven.

Hey nice article however, i

Hey nice article however, i went over to http://code.google.com/p/gwt-maven/ but i could not find an example pom configuration for the debug goal, this would come very handy for me but i have no idea how to set up the goal in my pom, it would be awesome if you could provide one!. thanks.

You don't need anything

You don't need anything special in your POM to use the debug goal, just setup a project (like the examples show, for example) and then try mvn gwt:debug.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.