Wednesday, April 25, 2012

Debug Java applications remotely with Eclipse



Remote debugging is a way of debugging any process running on some other location from your development machine. Local debugging is the best way in my opinion and should always be preferred over remote debugging but if local debugging is not possible and there is no way to debug your process then remote debugging is the solution.


Many of us work on a project which runs on Linux operating system and we do development mostly on Windows.
Eclipse provides us most useful feature called "Remote debugging" by using which you can debug your Linux running process from your windows machine.

Now let's see how we can setup remote debugging in Eclipse:

Just take a example of a simple program that we want to be debugged:


package com.tutoial.debugger;

/**
 * @author abdul
 *
 */
public class Debug {
    public static void main(String args[]) {
        for(int i=1; i<=10;i++) {
            System.out.println("2 * "+ i + " = " + 2*i);
        }
    }
}


Convert it into the jar.
From Eclipse , You can do it by following step:
1. Select the file that you want to export, Right click and choose "export".
2 .Goto Java-> jar and click Next
3. Browse the path where you want to export and click "finish".





Now, In order to remote debug a java application, Start your application with the following JVM debug options:

java -Xdebug -Xrunjdwp:transport=dt_socket,address=8000,server=y suspend=y -jar debug.jar


This will start java applicaiton debug.jar into debug mode using Java Debug Wire Protocol (jdwp) protocol and it will listen on port 8000 ,suspend=y will ensure that that application will not start running until eclipse connect it on speicified debug port.

After running it you should see something like that:
          Listening for transport dt_socket at address: 8000


Go to Eclipse and open debug dialog (you can press Alt+R, B). Create a new Remote Java Application Configuration. Select connection type as "Socket attach" and specify Connection Properties (for our example host would be localhost, and port would be 8000).




It also important to note that application must be start before eclipse tries to connect it other wise Eclipse will throw error "Failed to connect to remote VM. Connection refused" or "Connection refused: connect"

Enjoy..!!!!


Related link:

http://help.eclipse.org/helios/index.jsp?topic=%2Forg.eclipse.jdt.doc.user%2Ftasks%2Ftask-remotejava_launch_config.htm
http://www.ibm.com/developerworks/java/library/os-eclipse-javadebug/index.html?ca=dgr-jw22os-eclipse-javadebug/index.html&S_TACT=105AGX59&S_CMP=GRsitejw22

No comments:

Post a Comment

How TOPT Works: Generating OTPs Without Internet Connection

Introduction Have you ever wondered how authentication apps like RSA Authenticator generate One-Time Passwords (OTPs) without requiring an i...