Remote debug a Java application means the application run on a different VM rather than the one used for Eclipse. The Java application can run on the same host of you Eclipse as a separate process or on remote host.
In practical remote debug happens when there are some external environments in production that you don't have in your development environment. Or you code works in development environment, but has problems in production environment.
Remote debug can let you connect to the running applications and do what you can do for debug, like breakpoint/single-step-run/watch variables, just like debug a local application.
To make remote debug possible, extra JVM options have to be set when running the application remotely.
In this article, we create a hello world Java application, run it with command line, then use Eclipse to connect to it. In fact this hello world application can run on any remote host, but for demo it will run on the same host as where Eclipse is.
1. Create Project in Eclipse
Create a Java project in Eclipse with a main class.
package com.shengwang.demo;
import java.io.Console;
public class DemoMain {
public static void main(String[] args) {
Console console = System.console();
console.readLine("Please hit Enter to continue...");
System.out.println("Hello World! - Remote Debug in Eclipse");
}
}
The demo project only has only 1 DemoMain.java.
Compile the project and it's ready to run. In your case this step means you finish your project's coding in Eclipse.
2. Run with extra JVM options
To enable remote debug, application has to be run with extra JVM options.
-Xdebug -Xrunjdwp:server=y,transport=dt_socket,suspend=n,address=0.0.0.0:8000
JDWP is the agent for debug and it will listen on port 8000, which is the default debug port in most case.
3. Run Debug in Eclipse
Now the application is running, let's remotely debug it.
First select you project in Eclipse's package explorer. (make it easy to create debug configuration)
Then click "Run-> Debug Configurations" from menu. Double-click "Remote Java Application" to create a debug configuration for the selected project.
Make sure the host and port is where you application runs and debug server specified port. Then hit "Debug" to connect to the remote debug application. Almost done, you can debug the remote like you usually do. Let's set a break point on our source code.
Go back to our running application and hit enter to let it continue, the break point get hit and eclipse automatically switch Debug Perspective like local debugging.
0 comments:
Post a Comment