Introduction to Remote Java Debugging for Beginners
Published: 2021-09-21 01:44:27

## 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](https://www.datamation.com/cloud/end-user-public-cloud-spending/). 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](https://lightrun.com/debugging/remote-debugging/) Java projects using Lightrun's remote debugging tool, Lightrun Cloud.

## Setting up IntelliJ IDEA

According to the annual JVM ecosystem [report](https://snyk.io/blog/intellij-idea-dominates-the-ide-market-with-62-adoption-among-jvm-developers/) 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](https://lightrun.com/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](https://lh6.googleusercontent.com/rLSWvB4a8B69_L8z6eCWlOAhnKAHEOlKYut79QVDBu9COg30eyLvW-ZNWAYReBhW1_U8T43JQ1uUIr0w1UVfsJ7DqmGuMkcoOhH8_2_JLQax0Mt34z7X-qj5z3np-syjCkqVKYMu=s0)

In settings, go to Plugins > Marketplace.

![screenshot](https://lh3.googleusercontent.com/HiKS8N85YIuYxKkRAsR9wc1MT644flzmV_V1wHtHgpbeLQBScMnKjqxtA4sRD4GxOFKTCF5mfTn4O_nQ88hYB1lmTPbM5t6_6BcXx7PF5anrULKLQ9cAt0N3S1s-mLu416WKV684=s0)

Here, search for Lightrun to Install.

![screenshot](https://lh3.googleusercontent.com/hpWe0BKm6zyfCSLxzSEeVb1QKmmwoTU-zAGa9VsFtZfA2hvJgnPhFlC-RJGyfj7pDLQe396vl6QNqElkBu_REqxaZE4Pw3t-qorAdeSbafJg0Wdeh2Y-gymMMBgE_8pVs1FkZhCu=s0)

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](https://lh4.googleusercontent.com/c8xBxm-4aLNypkr92cKH7IBYB7aNo9rafNzGJnP8jjGYcYDNEaffQWd924hQFHrGwHMx-AUoxXW63vYbewEz3AVEEiInxkhTO8ntWqU5x6WyGKgsaburgdicb7acHuDcSvNHuEos=s0)

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");
}
```

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](https://lh6.googleusercontent.com/qul4ntcMl9r7sSd52B6LBrzczC1_iEeMZLcXUKx1cQTT28oIwOxP0Owj6l-TDkohBwk5ThGU78YIRiywjYky8HbUPVk4oXJM4mJLM8JB28dGpUB6yOA85PetZ2ug1sYs9SpOnVed=s0)

## 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](https://lh4.googleusercontent.com/KNSCS24JamfqdaSF1F4vmF9RRsJjA9SltTqszYi2bwslfjB7ML-ODrxKvMEdd5-xCDL6V0DXUdifaA0nkFVpO1WG3E1-esJUfi2W2cNeFAr2-8LDXR3N5HsT41L-S4oXGeOOJ2Zi=s0)

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

![screenshot](https://lh5.googleusercontent.com/SYksb4hG_cO3EFCXONFqt3snh3LZYN2LhIlIrrtOxKSEgbl0G2bkITfp4cSdVnGnNcAhwKuIUcJESxeLGjoxSMvCJU48hX-D_KtsQXP1bVi4r4LOfO2cig7ZHk_Vjm31M7P976-v=s0)

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](https://lh4.googleusercontent.com/d6UD8L0MBkwZQ2FwPMwEWJKYatZHpk5ZFwg4cEmIRiV8xgkbaR2ptFrQQ7J3qt6sH_3sj6XSjLhasy0gpeQzjU9v-TVzwK4IJyotX-Z4nF3N6EqeDQvbWROm5UqdhIItyrQJ_9EI=s0)

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

![screenshot](https://lh4.googleusercontent.com/wa84WmeP5PeQyPSJ1Sb5x2Yi3DbsTUj9YtdRfzUHva3WyypqT13oW_ebR03-7zI10RyuWi6VGv7IMXn-Qw7OqYZkYll-ldGjxQxVrl9SndasaCHDb5nqkU1W2o9zLcwq8_eA3TjL=s0)

## 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](https://lh6.googleusercontent.com/_fs6791jbnNShwJefEAaUAPO0OzGztHNs-FNHclt8sDlCR0XkR6T2zoyhX82QhqVD_GJXSOMQlq0t7slzd22Xwthud3daXCjhgjYDCMbWVrFc51-vqGv7pswKSUuIPKjJKnRNMOu=s0)

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](https://lh4.googleusercontent.com/qURM6P8KpOALY4fTJATmr1Cg4BJvPAH22wTcY4tI6owaHsrTVqr1RbI-7gLIdNeUlA-yYZ_fKPQFiPCLl6-zDSYMgmKSd4eol-8_MUS-PBLMa1jaE7gs3pr-WV7AvMcBxKYs8IBu=s0)

----
Cover Photo by [AltumCode](https://unsplash.com/@altumcode?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText) on [Unsplash](https://unsplash.com/s/photos/coding?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText)