1、Debug Java applications remotely with Eclipse Use the power of the Eclipse IDE to spread around your Java application debugging Level: Intermediate Charles Lu (), Software Engineer, IBM 09 Dec 2008 You dont need to debug Java applications on just your local desktop. Learn how to spread around your d
2、ebugging using different connection types that make up remote debugging. This article explains the features and examples that show how to set up remote application debugging. Remote debugging can be useful for application development, such as developing a program for a low-end machine that cannot ho
3、st the development platform, or debugging programs on dedicated machines like Web servers, whose services cannot be shut down. Other examples include Java applications running with limited memory or CPU power, such as mobile devices, or developers wanting to separate the application and development
4、environments, etc. Prerequisites If you dont have it already, download Eclipse V3.4 (Ganymede). In Ganymede, the socket listening connector has been added to the Remote Java Application launch-configuration type. Eclipses new socket listening connector allows you to start the Java debugger, which li
5、stens for a connection on a specific socket. The program being debugged can then be started with command-line options to connect to the debugger. Prior to the Ganymede release, only a socket-attaching connector was provided, and the program being debugged had to be a debug host that was connected by
6、 the debugger. It is impractical for mobile devices to be a host due to insufficient memory and CPU power. To use remote debugging, Java Virtual Machine (JVM) V5.0 or later must be used, such as IBM J9 or Sun Microsystems Java SE Development Kit (JDK). In this article, we focus on remote debugging,
7、rather than detail each of Eclipses debugging features. See Resources for more information about debugging with Eclipse and where to find the aforementioned software. JPDA introduction Sun Microsystems Java Platform Debugger Architecture (JPDA) technology is a multitiered architecture that allows yo
8、u to debug Java applications in all situations easily. The JPDA consists of two interfaces (the JVM Tool Interface and JDI, respectively), a protocol (Java Debug Wire Protocol), and two software components that tie them together (back-end and front-end). Its designed for use by debuggers in any envi
9、ronment. JPDA is not only for desktop systems but works well with embedded systems, too. The JVM Tool Interface (JVMTI) defines that a VM must provide for debugging. (Editors note: Starting with Java V5, JVMTI replaced JVMDI, which was used in Java V1.4.) The Java Debug Wire Protocol (JDWP) describe
10、s the format of debugging information and requests transferred between the process being debugged and a debugger front end, which implements the JDI, such as Eclipse, Borland JBuilder, and Launch-configuration type A launch configuration keeps a set of attributes that can be used to launch a program
11、. The launch-configuration type is a unique type of program that can be launched in the Eclipse platform. Frequently used acronyms JDI Java Debug Interface JDT Java Development Tools JDWP Java Debug Wire Protocol JPDA Java Platform Debugger Architecture JVM Java Virtual Machine JVMDI JVM Debug Inter
12、face JVMTI JVM Tool Interface VM Virtual Machine many others. The program being debugged is often called the debuggee in Suns JPDA specification. The JDI is a high-level interface to define the information and requests used for remote debugging. The architecture is structured as follows. Listing 1.
13、The Java Platform Debugger Architecture Components Debugger Interfaces / |-| / | VM | debuggee -( |-| - JVMTI - Java VM Tool Interface | back-end | |-| / | comm channel -( | - JDWP - Java Debug Wire Protocol | / |-| / | front-end | debugger -( |-| - JDI - Java Debug Interface | UI | |-| Therefore, a
14、ny third-party tools and VM based on JPDA should work together without complaint. This client-server architecture allows you to debug a Java program from a local workstation running the platform, or even debug it from a remote computer on your network. Before talking about the debug-scenario stuff,
15、we need to introduce two terms used in the JPDA specification: connector and transport. A connector is a JDI abstraction used to establish a connection between a debugger application and a target VM. A transport defines how applications access and transmit data between the front end and back end. Th
16、e connectors map to the available transport types and the modes of connection. In Suns reference implementation of JPDA, two transport mechanisms are provided on Microsoft Windows: socket transport and shared memory transport. Available connectors: Socket-attaching connector Shared-memory attaching
17、connector Socket-listening connector Shared-memory listening connector Command-line launching connector In establishing a connection between a debugger application and target VM, one side acts as a server and listens for a connection. At some later time, the other side attaches to the listener and e
18、stablishes a connection. The connections allow the debugger application or the target VM to act as a server. The communications among processes can be running on one machine or different machines. The problem with debugging a Java program remotely is not in the debugger front end but the remote Java
19、 back end. Unfortunately, there is not much information about this in the Eclipse help system. In fact, JDI and JVMTI are implemented by Eclipse and the Java runtime environment, respectively. The only thing we are concerned with is the JDWP, which contains the information to communicate with the JV
20、MTI and JDI. The JDWP contains many arguments that have been added to invoke the application for the remote Java application. Following are some of the arguments used in this article. -Xdebug Enables debugging features. -Xrunjdwp: Loads the implementation of JDWP in the target VM. It uses a transpor
21、t and the JDWP protocol to communicate with a separate debugger application. Specific suboptions are described below. Starting from Java V5, you can use the -agentlib:jdwp option, instead of -Xdebug and -Xrunjdwp. But if you have to connect to the VM prior to V5, -Xdebug and -Xrunjdwp will be the on
22、ly choice. Following are brief descriptions of the -Xrunjdwp suboptions. transport Generally, socket transport is used. But shared-memory transport can also be used on the Windows platform, if available. server If the value is y, the target application listens for a debugger application to attach. O
23、therwise, it attaches to a debugger application at the specified address. address This is the transport address for the connection. If the server is n, attempt to attach to a debugger application at this address. Otherwise, listen for a connection at this port. suspend If the value is y, the target
24、VM will be suspended until the debugger application connects. For detailed explanations for each debug setting, refer to the JPDA documentation (see Resources). Listing 2 shows an example of how to launch a VM in debug mode and listen for a socket connection at port 8765. Listing 2. Target VM acts a
25、s a debug server -Xdebug -Xrunjdwp:transport=dt_socket,server=y,address=8765 Listing 3 shows how to attach to a running debugger application using a socket on host 127.0.0.1 at port 8000. Listing 3. Target VM acts as a debug client -Xdebug -Xrunjdwp:transport=dt_socket,address=127.0.0.1:8000 Remote
26、debugging features in Eclipse Eclipse is a graphical Java debugger front end. The JDI is implemented in org.eclipse.jdt.debug bundle. In this article, we dont discuss the details of JDI implementation. See Resources for information about Eclipse JDT and Java JDI technology. The first thing we want t
27、o know is which Eclipse connector to use. To learn the remote connection types provided by Eclipse, you can add a launch configuration in Remote Java Application by going to the Eclipse menu and selecting Run Debug Configurations., then selecting the connector from the dropdown list. Two connectors are provided in Ganymede: