After my previous article about Remote Debugging .NET Applications using Visual Studio 2010, I was curious to see how Remote Debugging works for Java Applications. This blog post covers Remote Debugging Java Applications using NetBeans 7.0.
The basic concepts of Remote Debugging are the same for .NET and Java but the process of setting up the host and remote computers varies between Visual Studio and NetBeans. Similar to the .NET application used in the previous post, the screen shots correspond to a simple Java application which would popup a MessageDialog on a button click. Get the code here.
Before configuring the host and remote computers, it is vital to understand the Java Platform Debugger Architecture (JPDA). JPDA provides the infrastructure you need to build end-user debugger applications for the Java Platform. It includes the following APIs broken into three layers -
- Java Debug Interface (JDI), a high-level Java programming language interface including support for remote debugging
- Java Debug Wire Protocol (JDWP), which defines the format of information and requests transferred between the process being debugged and the debugger front end
- JVM Tools Interface (JVM TI), which is a low-level native interface that defines the services a JVM provides for tools such debuggers and profilers
Configuring the Remote Computer
Run the Java application using the -Xdebug and -Xrunjdwp options from the command line.
java -Xdebug -Xrunjdwp:transport=dt_socket,address=6000,server=y -jar RemoteDebugging.jar
Here's a description of the options of the java command -
-Xdebug | Enables debugging support in the VM |
-Xrunjdwp | Loads in-process debugging libraries and specifies the kind of connection to be made |
The -Xrunjdwp option has several sub options. Here are the descriptions of the ones that are used above -
transport | Name of the transport to user in connecting to debugger application |
address | Transport address for the connection If server=n, attempt to attach to debugger application at this address If server=y, listen for a connection at this address |
server | If y, listen for a debugger application to attach If n, attach to the debugger application at the specified address |
By default the application starts in suspended mode. In suspended mode the application waits for a debugger to attach itself to the server at the specified port before the application starts.
Configuring the Host Computer
The host computer is the system running NetBeans 7.0. Open the code of the application in NetBeans and select "Attach Debugger". Specify the Connector as SocketAttach, the Host as the hostname of the remote system and the Transport and Port as specified above.
Specify the breakpoints in the code and they would hit appropriately. There are two major bottlenecks in Remote Debugging -
- The code from which the executable was built should be available at the time of debugging
- Applications cannot be configured for Remote Debugging at runtime. The -Xdebug option must be specified at the instantiation of the application, making debugging live production code difficult
Before I conclude, here's an article from OTN (Oracle Technology Network) on the Java Platform Debugger Architecture. Visit it if you would like a deeper insight into Java Debugging. The schematic of the JPDA is from a weblog, check it out here.