6/16/2012

Remote debugging with eclipse

Debugging java application or web application on server is very painful with a lot of reasons. Here I set the server and client the same computer.

I used eclipse here so make sure you installed eclipse

Setup Server:
Assume you've finished your java code and looks like below:

public class ForInterview {
        public static void main(String[] args) {
String a = "ARMZM2G002825";
float b = genIndex(a);
float d = (float) 5.020802E24;
System.out.println(b == d);
int c = 'Z' - '\u0030';
System.out.println(c);
    }
}

Export the project/ForInterview.jar into some directory where you regard as the server. You can save it on your local machine or remote server. 

Then run 
    java -Xdebug -Xrunjdwp:transport=dt_socket,server=y,address=8000 -jar    ForInterview.jar 
You'll see Listening for transport dt_socket at address: 8000
Now the server is waiting for debug command. 
// From java5 you can run  -agentlib:jdwp instead of -Xdebug -Xrunjdwp

Setup Client:
Right click on eclipse Run->Debug Configuration->Remote Java Application and setup the client as the graph below:


You can change whatever Host or Port as the server needed. But I think if the server require permission, there might be a problem, I haven't try that.

1 comment: