Skip to main content

How to remotly debug java program using Eclipse

Eclipse provides a facility to debug your java programs remotely.
To demonstrate how you can remotely debug java programs remotely, I will do the following:
  1. Create a .bat file that calls the java program
  2. Configure the arguments to the JVM so that the JVM will be capable of being remotely debugged
  3. Configure Eclipse to connect to the remote JVM
1. Create a bat file calling the java program
I create a java class given below:
public class RemoteDebugDemo {
    public static void main(String[] args)
    {
        System.out.println("STEP 1");
        System.out.println("STEP 2");
        System.out.println("STEP 3");
        System.out.println("STEP 4");
        System.out.println("STEP 5");
        System.out.println("STEP 6");
    } 
}
Now I wish to run this program in batch mode.So I write a file called RemoteDebugDemo.bat file with following content.
java -jar RemoteDebugDemo.jar
I create a jar file named RemoteDebugDemo.jar containing the RemoteDebugDemo class.
(If you wish to know how to create a jar file using eclipse, you can refer this article on how to create jar files using eclipse).
2. Configure the parameters to the JVM so that the JVM will be capable of being remotely debugged
Now to remotely debug the RemoteDebugDemo.bat file, I modify the batch file content as follows:
java -Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=1044 -jar RemoteDebugDemo.jar
These are the java debugger jdb parameters.
The transport = dt_socket instructs the JVM that the debugger connections will be made through a socket.The address=1044 parameter indicates the port number. suspend=y means that the jvm will start in suspended mode and will stay in suspended mode until the debugger is attached to it.
In this case my JVM is on localhost.It can lie anywhere in the network.You will need to know the IP address and the port number of the host for enabling remote debug.
Now I place the 2 files RemoteDebugDemo.jar and RemoteDebugDemo.bat in folder "C:\amol\" and run the batch script as shown below.


A message is displayed
'Listening for transport dt_socket at address :1044'
.
3.Configure Eclipse to connect to the remote JVM
Now I need to connect my eclipse code to this JVM so that I can debug the code.
In Eclipse, I place a breakpoint on the first line in the main method.This is done so that once the eclipse debugger is connected to the remote JVM, it should suspend at the breakpoint.
After this, I open the "debug configuration" screen in eclipse.
Run -> Debug configuration
Here click the 'Remote Java Application' and click 'New'.
Here I specify following
host : localhost port :1044
This means that I want to connect to the JVM running on localhost and listening to port 1044 for incoming connections.
Specify the details as shown in the screen shot below:


On clicking the debug button, a connection is established to the java program running in batch mode and the code can be debugged.
You can use this remote debug feature in situations like
  • Debugging in eclipse while running the actual application in windows command prompt in same machine or a distant machine in the network
  • Debugging in eclipse while running the actual application in unix or unix based OS
  • Debugging in eclipse in windows and the program running on a distant unix machine in the network
Let me know if this article was useful to you. Also, feel free to share your knowledge about remote debugging in the form of comments below.

Comments

  1. where I need to set this parameters ?
    java -Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=1044 -jar RemoteDebugDemo.jar
    on Linux box ?
    getting below error

    -Xdebug: illegal argument
    Usage: java [-options] class

    ReplyDelete
    Replies
    1. You need to give these parameters as arguments to the JVM, just like you give the classpath.You got that error because probably you typed it incorrectly.Try changing the order of the arguments.

      Delete

Post a Comment

Have some feedback, comment or question ?
Post it here in comments section.

Popular posts from this blog

Five most useful spring modules

  Java Spring framework is one of the popular java frameworks. A thread on 5 commonly used spring modules 1. Spring Boot and Spring MVC These modules makes it easy and quick to create stand-alone, production-grade Spring based Applications that you can "just run". Example Use:- A RESTful service with self-container server. 2. Spring Batch This module is used to build batch applications that need to recurrently process large volumes of data. Features: Chunk based processing, retry, skip Example Use:- Everyday, read data of daily trades from database and update the credit on customer's accounts. 3.Spring Data Provides a spring based programming model for accessing data. This module has very powerful features and supports wide range of databases. Example Use:- Read, Write data from PostgreSQL for your application. 4.Spring Cloud Provides tools for developers to quickly build some of the common patterns of distributed systems. Libraries for Service registration and discovery,...

Five important aspects of microservices

  5 important aspects/components of microservices architecture 1. Microservices - Services that are built small enough so that their entire lifecycle can be owned and managed by a small team. 2. Service Discovery - A mechanism of how microservices can locate each other over network.  Example : if Microservice A wants to call microservice B:  It will use service discovery mechanism to fetch IP address and port of B and then call B. Implementations : ZooKeeper, Consul, etcd, Eureka 3. External configuration - Enable services to run on various environments without a code modification inside the service. Enable different configuration values for those services for different environments.  Dynamically change configurations without changing application code or restart/redeploy application. Example - DB credentials and other details, application properties, environment variables for Dev, QA and Prod env. Implementations : Consul, Spring cloud (enables various datastores ...

More about Me

You can find more about me on my linkedIn profile  https://www.linkedin.com/in/amolrlimaye/   I have written a blog series on microservices which you can read here  https://medium.com/@amol.limaye   My open source contributions are less, but you can see my github activity here  https://github.com/amollimaye