DEV Community

Cover image for Introduction to Remote Java Debugging for Beginners
Dori_an
Dori_an

Posted on

Introduction to Remote Java Debugging for Beginners

Introduction 

Most of us developers use our trusty print statements to debug the application, but analyzing the program over and over, adding/removing a bunch of print statements can be time consuming as well as exhausting. So, a debugger lies on the top of any developer's toolbox

Debugging has come a long way with time. From Admiral Grace Hopper finding a moth in her system impacting her program in the 1940s and coining the term "Debugging", computer programs nowadays are more complicated. And with complexity, it becomes even much harder to find bugs in a huge codebase.

This makes the need more pressing for a robust debugger which not only helps developers find bugs, but also perform debugging without affecting deployed running programs on remote systems.

With debugging, it becomes easier to understand someone else's code.

In 2020, total spending on cloud services by end-users was $270 billion. With so many people adopting the cloud for its convenience, it is not surprising to know that developers are now developing their applications remotely on cloud servers.

Though there are many benefits to developing Java applications remotely on cloud servers, the question of efficiently and correctly debugging arises. This is where remote debugging comes.

In this article, we will take a look at remote debugging Java projects using Lightrun's remote debugging tool, Lightrun Cloud.

Setting up IntelliJ IDEA

According to the annual JVM ecosystem report by Snyk, 62% of developers use IntelliJ IDE as their main IDE (integrated development environment). IntelliJ provides higher developer productivity as well as increased testing and debugging productivity with improved testing plans.

Therefore, we will be using a free-to-use production debugger called Lightrun Cloud. It has logging capabilities and staging environments and operates on the IntelliJ IDE for Java applications. It is great for Monolith, microservices, Kubernetes, Docker Swarm, ECS, Big Data workers, serverless, and more such applications.

There are multiple ways to set up Lightrun in your environment. The best way is to install it directly from the IntelliJ marketplace.

Go to File > Settings.

screenshot

In settings, go to Plugins > Marketplace.

screenshot

Here, search for Lightrun to Install.

screenshot

After a restart, Lightrun will be set up in your environment.

Creating Demo Code

To add demo code, create a new java class file under src.

screenshot

Here, add the following demo code to debug.

public static void main(String[] args) {
    System.out.println("Starting");
    for (char c = 'A'; c < 'Z'; c++) {
        try {
            Thread.sleep(2500);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(c);
    }
    System.out.println("Complete");
}
Enter fullscreen mode Exit fullscreen mode

This code simply prints alphabets, but it is enough to demonstrate debugging.

Configuring Lightrun

Now, we will need to add a new run configuration to properly run our debugger on the code.

Navigate to Run > Edit Configuration. That should open a new window.

Click on the + icon to create a new configuration and choose remote JVM Debug.

Now, fill in the details as follows:

Assign a name to the configuration. In this case, we have called it "lightrun-test".

The host and port sections are the most important part of the configuration as they tell your IDE where to connect for the remote debug.

Add the IP address of your remote system in the host section as well as the port of the service listening for remote debugging. Here, for the testing, we are putting localhost to specify we are connecting to localhost. You can keep command line arguments to default or change as needed. Copy the command line to a file for later use.

screenshot

Packaging the code in a JAR file

Now that Lightrun is configured, we need to create a JAR file to run remotely. Open File > Project Structure and click on Artifacts.

Click on +, click on JAR, and click "from the module with dependencies".

screenshot

On the right side of the screen, click on the icon and select the class name, apply changes, and close.

screenshot

Now, you can build by going to Build and clicking on Build Artifacts. Next, click on  "Create JAR" and click on Build. Now, you should have a JAR file in the out/artifacts folder.

screenshot

Now, you can directly run the JAR using a command line with the string we copied before:

screenshot

Adding Configuration for The Running Application

The next step is adding a new configuration for the application.

Go to Run > Edit Configuration and add a new configuration under Application.

Click on Modify Options and ADD VM options.

screenshot

Later, in the field of VM options, paste the string containing arguments.

Now, you can directly run the application as well as debug it remotely by adding breakpoints.

screenshot


Cover Photo by AltumCode on Unsplash

Top comments (0)