Wednesday, 18 February 2015

How to debug remote Java web application using eclipse

Today most of the web applications are developed using java. While developing java applications, debugging is the process by which we can not only save our time and efforts but also increase productivity. Debugging involves local debugging and remote debugging. I will always prefer local debugging over remote debugging. But there are scenarios where local debugging is not possible and we have to go for remote one. But the good news is that today most of the IDE like Netbeans, Eclipse etc. supports remote debugging.

In this post we will learn how to debug java web application deployed using tomcat on remote machine. Both local and remote machine should have windows 7.
To debug remotely the most important thing is that we need source code of our application on our local machine. 
We need to perform two simple steps which will not take much of your time.
            1)    Start the tomcat in debug mode on remote machine.
            2)    Connect local eclipse to remotely running tomcat server  

    1)   Start the tomcat in debug mode on remote machine

        To start the tomcat in debugging mode you need to perform following 3 simple steps:

                1)    Copy following two lines and paste them in startup.bat at the first line after 
                  set JPDA_ADDRESS=8000
                  set JPDA_TRANSPORT=dt_socket

               2)    Change last line in startup.bat
                 call "%EXECUTABLE%" start %CMD_LINE_ARGS%             to
                 call "%EXECUTABLE%" jpda start %CMD_LINE_ARGS%.

               3)    Now run the tomcat from command prompt using following command:
          This will start the tomcat in debugging mode. Now we only need to create run confugarion in elipse and connect to the tomcat we have just started.

2)   Connect local eclipse to remotely running tomcat server

To create the run configuration in eclipse perform following simple steps:

1)    Open eclipse IDE. Click on Run -> Debug Configurations… Please refer to following image 

2)    Now Select Remote Java Application in the list and create new configuration. Please refer to the following image :

3)    This will open following window. Now browse for the application which you want to debug, fill up the IP address of remote machine on which you tomcat is running as a host and port number to which you want to connect. (Default port number of tomcat is 8080). 
Please refer to the following image:

4)    That’s it. Your debug configuration is ready. Now click of debug button. This will connect to the remote machine. To start debugging open the browser and go to your application. This will heat the breakpoints in eclipse.

“Failed to connect to remote VM. Connection refused.”

    Most of the time developer face “Failed to connect to remote VM. Connection refused” issue while debugging remotely. This means that eclipse refused to connect to the specified machine on the specified port number. There can be many reasons why you are facing this issue. Here I have listed some of them:
1)      Most probably may be your application is not running on the remote machine and hence you cannot connect to the machine. To check this out just restart the tomcat in debug mode and try to connect again.
2)      May be your firewall preventing you from connecting to the remote machine.
3)      May be host or port number are incorrect.