Java Debug Wire Protocol (JDWP)
The Java Debug Wire Protocol (JDWP) is a component of the Java Platform Debugger Architecture (JPDA). It defines the communication between a debugger and a Java Virtual Machine (JVM) that is running a program being debugged. JDWP is used to perform debugging tasks such as setting breakpoints, stepping through code, and inspecting variables.
Key Components of JDWP
JDWP works alongside other components of the JPDA:
- Java Debug Interface (JDI): A high-level Java API for writing debugger applications.
- Java Virtual Machine Tool Interface (JVM TI): A native programming interface used by the JVM and debug agents.
- JDWP: The protocol used for communication between the debugger and the JVM.
How JDWP Works
JDWP operates over a network or a local connection, using a client-server model. The JVM acts as the server, and the debugger acts as the client. This setup allows remote debugging, where the debugger can run on a different machine than the JVM.
Enabling JDWP
To enable JDWP for a JVM, you typically use the -agentlib:jdwp option when starting the JVM. Here's an example:
java -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=*:5005 -jar MyApplication.jarIn this example:
transport=dt_socket: Specifies the transport mechanism (socket in this case).server=y: Indicates that the JVM should listen for a debugger connection.suspend=n: The JVM should not wait for a debugger to connect before starting.address=*:5005: The address and port on which the JVM listens for debugger connections.
JDWP in Action
Let’s go through an example to see JDWP in action. We will create a simple Java application and set up JDWP to debug it.
Step 1: Create a Simple Java Application
Create a file named MyApplication.java with the following content:
public class MyApplication {
public static void main(String[] args) {
int a = 10;
int b = 20;
int sum = add(a, b);
System.out.println("Sum: " + sum);
}
public static int add(int x, int y) {
return x + y;
}
}Step 2: Compile the Application
Compile the Java application using the javac command:
javac MyApplication.javaStep 3: Run the Application with JDWP
Run the application with JDWP enabled:
java -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=*:5005 MyApplicationThe JVM will start and listen for a debugger connection on port 5005.
Step 4: Debugging the Application
To debug the application, you can use any JDWP-compliant debugger. Here, we’ll use jdb, the command-line debugger that comes with the JDK.
Get Mahad’s stories in your inbox
Join Medium for free to get updates from this writer.
Open a new terminal and start jdb with the following command:
jdb -attach localhost:5005Once jdb is connected, you can set breakpoints and inspect the state of the application. For example, to set a breakpoint at the add method and continue execution, use the following commands:
> stop in MyApplication.add
Deferring breakpoint MyApplication.add.
It will be set after the class is loaded.
> run
VM Started: Set deferred breakpoint MyApplication.add
Breakpoint hit: "thread=main", MyApplication.add(), line=11 bci=0
11 public static int add(int x, int y) {
> print x
x = 10
> print y
y = 20
> cont
Sum: 30
The application exitedSample Code with Debugging Statements
Here’s an enhanced version of MyApplication.java with additional print statements to help with debugging:
public class MyApplication {
public static void main(String[] args) {
int a = 10;
int b = 20;
int sum = add(a, b);
System.out.println("Sum: " + sum);
}
public static int add(int x, int y) {
System.out.println("Adding " + x + " and " + y);
return x + y;
}
}Summary
JDWP is a crucial part of Java’s debugging capabilities, allowing developers to connect debuggers to running JVMs for inspecting and controlling the execution of applications. By using JDWP, you can set breakpoints, step through code, and monitor variable states, making it easier to diagnose and fix issues in Java programs.










