Use eclipse to remotely debug Java applications

Source: Internet
Author: User
Tags connect socket java se

Document options
& Lt; tr
Valign = "TOP" & gt; & lt; TD width = "8" & gt; & lt; IMG alt = "" Height = "1" width = "8"
Src = "// www.ibm.com/ I /c.gif"/& gt; & lt;/TD & gt; & lt; TD width = "16" & gt; & lt; IMG alt = "" width = "16"
Height = "16" src = "// www.ibm.com/ I /c.gif"/& gt; & lt;/TD & gt; & lt; TD class = "small"
Width = "122" & gt; & lt; P & gt; & lt; SPAN class = "Ast" & gt; Javascript is not displayed
Documentation options & lt;/span & gt; & lt;/P & gt; & lt;/TD & gt; & lt;/tr & gt;


Print this page

Send this page as an email

Original ENGLISH

Level: Intermediate

Charles Lu, software engineer, IBM

January 12, 2009

Debugging Java applications on a local computer is not the only option. Learn how to use different connection types that constitute remote debugging for remote debugging. This topic describes how to set Remote Application debugging.


Remote debugging is very useful for application development.
For example, develop programs for low-end machines that cannot host the development platform, or debug programs on dedicated machines (such as Web servers that cannot interrupt services. Other situations include: Small memory or
Java applications (such as mobile devices) On devices with low CPU performance, or developers want to separate the applications from the development environment.

Prerequisites

STARTUP configuration type
STARTUP configurationSave a set of attributes used to start the program. The STARTUP configuration type is a unique program that can be started on the eclipse platform.

For example
If you have not installed this program, download eclipse v3.4 (Ganymede ). In Ganymede, the socket listening connector is added
Remote Java application startup configuration type. Eclipse's latest socket listener connector allows you to open Java
Debugger, which can listen to connections on a specific socket. You can open the program to be debugged from the command line option and connect it to the debugger. In Ganymede
Before the release, there was only one socket connection. The machine where the program to be debugged resides must be a debugging host connected to the debugger. Due to memory and CPU
It is unrealistic to allow a mobile device to act as a host.

To perform remote debugging, you must use Java Virtual Machine
(JVM) V5.0 or an updated version, such as Java SE development of IBM j9 or Sun Microsystem
Kit (JDK ). This article focuses on remote debugging, rather than the details of each eclipse debugging feature. View references for more information about Debugging Using eclipse and find the software mentioned above.

JPDA Overview

Common acronyms
  • Jdi
    -Java debug Interface)
  • Jdt
    -Java Development Tools)
  • Jdwp
    -Java debugging network protocol (Java debug wire protocol)
  • JPDA
    -Java platform debugger architecture (Java platform debugger Architecture)
  • JVM
    -Java Virtual Machine)
  • Jvmdi
    -JVM debug Interface)
  • Jvmti
    -JVM tool interface)
  • VM
    -Virtual Machine)

Sun
Microsystem's Java platform debugger architecture (JPDA)
The technology is a multi-layer architecture that enables you to easily debug Java applications in various environments. JPDA consists of two interfaces: JVM tool interface and
Jdi), a protocol (Java debug Wire
Protocol) and two software components (backend and front-end) used to merge them. It is designed to allow debugging personnel to debug in any environment. JPDA
Not only can it be used in desktop systems, but also can work well on embedded systems.

JVM tool interface (jvmti) specifies that the VM (Edit note:Slave
Java V5 will replace jvmdi in Java v1.4 with jvmti ). Java debug wire protocol
(Jdwp) describes the debugging information format and the request transmitted between the debugged process and the debugger front-end. The debugger front-end implements jdi, such as Eclipse and Borland.
JBuilder. According to Sun's JPDA specification, the program to be debugged is often calledDebuggee. Jdi is an advanced interface that defines information and requests for remote debugging. The architecture of the debugger is given below.

Listing 1. 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, any third-party tool and JPDA-based VM should be able to coordinate the work. With this client-server architecture, You can debug Java programs from the local workstation running the platform, or even remotely debug programs through the network.

In
Before discussing the debugging scenario, we should first understand two terms in the JPDA specification: connector and transmission. The connector is a jdi abstraction used in the debugger application and target VM
Establish a connection between them. Transmission defines how applications access and how data is transmitted between the frontend and backend. The connector maps to available transmission types and connection modes. In Sun's
JPDA provides two transmission mechanisms for Microsoft Windows: Socket transmission and shared memory transmission. Available connectors:

  • Connect socket connector
  • Connect to the Shared Memory Connector
  • Listener socket connector
  • Listen to Shared Memory Connector
  • Start the command line connector

When a connection is established between the debugger application and the target Vm, one end is used as a server and listens for the connection. Then, the other end connects to the listener and establishes a connection. Through the connection, the debugger application or the target VM can act as a server. Communication between processes can run on the same machine or different machines.

Yes
Remote debugging of Java programs is not difficult at the front-end of the debugger, but rather remote Java backend. Unfortunately, eclipse
The help system does not provide much information for this purpose. In fact, jdi and jvmti are implemented by the runtime environment of eclipse and Java respectively. We only need to consider
Jdmp, because it contains the information required to communicate with jvmti and jdi. Jdwp contains many parameters for remote Java
The application calls the required program. The following are some parameters used in this article.

-Xdebug
Enable the debugging feature.
-Xrunjdwp:<Sub-options>
Load jdwp in the target Vm for implementation. It communicates with independent debugger applications through the transmission and jdwp protocols. The following describes some specific sub-options.

You can use-Agentlib: jdwpOption, instead-XdebugAnd-Xrunjdwp. However, if you connect to a VM before V5, you can only select-XdebugAnd-Xrunjdwp. The following is a brief description.-XrunjdwpSub-options.

Transport
Socket transmission is usually used here. However, shared memory transmission can also be used on Windows platforms.
Server
If the value is YThe target application listens to the debugger application to be connected. Otherwise, it will connect to the debugger application at a specific address.
Address
This is the connection transfer address. If the server is NWill try to connect to the debugger application on this address. Otherwise, the connection will be monitored on this port.
Suspend
If the value is Y, The target VM will be paused until the debugger application connects.

For detailed explanation of each debugging setting, see the JPDA documentation (see references ).

Listing 2 is an example of how to start the VM in debug mode and listen to socket connections on port 8765.

List 2. Target Vm for debugging server


-Xdebug -Xrunjdwp:transport=dt_socket,server=y,address=8765

Listing 3 shows how to use a socket on host 127.0.0.1 on port 8000 to connect a running debugger application.

Listing 3. Target Vm for client debugging


-Xdebug -Xrunjdwp:transport=dt_socket,address=127.0.0.1:8000


Back to Top

Remote debugging features in eclipse

Eclipse is a graphical Java debugger front-end. Jdi is implemented in the org. Eclipse. jdt. debug package. This article does not discuss jdi implementation in detail. See references for information on Eclipse jdt and Java jdi technologies.

First, we should know which eclipse connector to use. To learn about the remote connection type provided by ECLIPSE, go to the eclipse menu and selectRun> debug deployments..., InRemote Java ApplicationAdd a STARTUP configuration and select a connector from the drop-down list. There are two connectors in Ganymede:

  • Socket attach
  • Socket listen

Pair
For the connector for listening sockets, eclipse VM will be the host connected to a remote Java application. For the connector connected to the socket, the target VM
Will be used as a host. These two connectors have no impact on application debugging and you can choose any one. However, based on experience, we need to use a faster and more powerful computer as a VM.
Debug the host because there are a lot of computing resources.

When debugging Java
Before the application, make sure that all debugging options are enabled for the remote application. If the option Information is unavailable, you will receive an error message, such as "debug information
Is not available "or" unable to install breakpoint due to missing line
Number ". You can changeWindow> preferences> JAVA> CompilerTo modify the settings.

Figure 1. debugging options in eclipse



Back to Top

Remote Application debugging

Now, we are ready to remotely debug the application. Perform the following steps:

1.Create a Java project using simple classes
We create a simple class for debugging. Listing 4 provides sample code.

Listing 4. debugging sample code


package com.ibm.developerWorks.debugtest;

public class test {

public static void main(String[] args) {
System.out.println("This is a test.");
}
}

2.Set a breakpoint
Set a breakpoint in the code. In this example System.out.println("This is a test.");Set a breakpoint in this line.

Figure 2. set breakpoints in eclipse

3.Debug the application locally
Before debugging the application, make sure that the debugging options described in Figure 1 are enabled for the project. It is unnecessary to debug the application locally, but this ensures that all debugging information is available. Right-click the Java project and select DebugAnd then select Java application(See figure 3 ). If the application stops running at the breakpoint, the debugging information is correctly displayed. In this case, you can continue to use these debugging features, such as displaying the debugging stack, variables, or breakpoint management.

Figure 3. debug the application locally

4.Export a Java Project
We will use this application as the debugging target. Right-click the Java project and choose ExportAnd then select Java, Finally select JAR FileOr Runnable JAR FileExport the project. The jar file will be generated at the specified location. Note: If the Java source code does not match the target application, the debugging feature will not work properly.
5.Run the Java application manually
Open the console and manually start the application to ensure that the Java Runtime Environment is correctly configured.

Listing 5. Example of calling a Java application


java -jar test.jar

6.Remote Application debugging
Copy the JAR file to a remote computer or a proper location on the same computer, call the debugging server, and then add a client to it. A simple Java application can act as a debugging server or client. You can select Socket attachOr Socket listenThe connection type depends on the specific configuration. The following two sections will show you how to run an application as a server or client.

As the target Vm of the debugging server

The following example remotely calls a Java application as the debugging server and listens to socket connections on port 8000. The target VM is paused until the debugger is connected.

Listing 6. VM call example in eclipse connection Socket mode


java -Xdebug -Xrunjdwp:transport=dt_socket,server=y,address="8000" -jar
test.jar

Use the remote STARTUP configuration to start eclipse and specify the target VM address of the Remote Application. To do this, clickRun> debug deploymentsIn the eclipse menu, double-clickRemote Java application. Specify the IP address and port for the target application from the newly created STARTUP configuration. To run remote applications on the same machine, you only need to specify the Host IP address as localhost or 127.0.0.1.

Figure 4. Socket Connection Configuration

SelectAllow termination of remote VMOption to terminate the VM connected during application debugging.

Figure 5. Terminate button in eclipse

Target Vm for client debugging

The second example uses a simple Java application as the debugging client, and the front-end of the debugger as the debugging server. Eclipse uses the socket listening mode to listen to the connection type. You must first start the debugging front-end to listen to specific ports. Figure 6 shows an example configuration for setting a listener.

Figure 6. Listener socket connection Configuration

Click eclipseDebug
The status bar displays the message "waiting for VM to connect at Port ".
8000 ...". Start the remote application after you see the message. Listing 7 shows how to use a Java application as a debugging client and call it, and then use port 8000
The socket on host 127.0.0.1 connects it to a running debugger application.

Listing 7. Examples of VM calls in socket connection mode of eclipse listeners


java -Xdebug -Xrunjdwp:transport=dt_socket,address=127.0.0.1:8000,suspend=y
-jar test.jar

If everything goes smoothly, a debugging perspective is displayed to help debug the application, and the remote Java application stops working normally. This is similar to local debugging in step 3 (see figure 3 ). Here, you can use standard debugging features, such as setting breakpoints and values, and performing one step.



Back to Top

Conclusion

This article demonstrates how to use the remote Java application configuration type built in eclipse to remotely debug the application. Describes how to set up a Java application to call remote debugging and helps you understand the connector provided by eclipse. Finally, you learned how to apply these technologies to projects.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.