Remote debugging Java using Visual Studio Code. Have you tried yet?

Tram Ho

Implementing java code on Visual Studio Code is probably not an option for many people, because there are many IDEs that support very good java programmers like Intelij, eclipse,… However, if your purpose Not a programmer but just want to debug and see how the code works, then this article is for you. In this article, I will guide you to debug and remote debug java applications with Visual Studio Code. The reason why I choose Vs code, is partly because it is lightweight, supports many interesting plugins and partly because of my personal taste. Ok, let’s start with debugging a simple java code.

Debug java application

To be able to debug java application, first we need to install JDK on our computer. After installing the JDK, you will need to configure the environment for Java. The most common way is to set the JAVA_HOME environment variable to the installation location of the JDK. Next we need to install 2 plugins, Debugger for Java and Language Support for Java(TM) by Red Hat .

First let’s create a simple java program named Test.java with the following content:

If you install the correct java environment, then we can run or debug the above application by clicking the button as shown below.

The output after running the program is as follows:

image.png

So what about debug, to debug we need to put BreakPoint at a certain line (here I put it at line 14) then we click on Debug

The program will run as usual until it reaches the BreakPoint , VSCode will highlight the line where we set the BreakPoint

Here we can observe some values ​​such as the values ​​we entered for the two variables a and b (can be viewed at line 14 or in the left Variables window). Also here we will notice that we will see a toolbar at the top including some of the following functions: Continue, Step Over, Step Into, Step Out, Restart, Stop ... . We will go into detail about each function

  • Continue : The program will continue to execute until it finishes or encounters another BreakPoint .
  • Step Over : The program will execute the line of code we are standing on and pause at the next line of code (In my case here, after clicking Step Over, the program will execute the Add method, print to the screen, then which pauses at the 15th line).
  • Step into : allows the program to jump into the method being called (In this case, after clicking Step into, the program will jump into a series of jdk methods first and then jump into the Add method and stop at the second line. 24). This is an inconvenience compared to intelij because using step into in intelij, we can choose which method to jump into, thereby ignoring methods we don’t need to care about. In vs code, we have to go in turn in the order of execution of the program.
  • Step Out will finish executing the current method and then stop where the method returns the result. To be more clear, if we click Step out while we are in main(), the program will execute and stop, otherwise if we click step out while in the Add method, the Add method will be executed and the program will be executed. return pauses at line 14.

Now we come to this more interesting part.

Remote debugging java with Visual Studio Code

Create project and build jar file

First, we will create a java project by clicking the following button:

At this point we have many options such as creating a spring boot, maven or gradle project. Here, for the sake of simplicity, I just created a console project.

image.png

After we have created the project, the result will be as shown above. You edit the App class with the same content as the Test.java file that I left at the beginning. Next we will build this project into a jar file.

image.png

Build jar successfully, we will see the Test.jar file appear in the folder as follows

image.png

Remote debugging

One disadvantage of VsCode when doing remote debugging is that we must have the full source code of the program to be able to debug it. If we only have the jar file, then we won’t be able to remotely debug the application, then intelij would be more appropriate choice.

First we need to create config for remote debugging by clicking Run and Debug then click create a launch.json file

Next we click on Add Configuration and select Java: Attach to remote Program .

Config will appear, you will need to fill in some information as follows:

  • hostName: is the ip address of the machine running the server you want to debug (here, I run it on my machine, so it will be localhost)
  • port: is the debug port that I install to be able to remote debug (here I leave it as 5005)

image.png

Now we have successfully configured VsCode. Now we will run the jar file we created from the previous step with the debug option enabled.

After running up, we receive the message Listening for transport dt_socket at address: 5005 that the program is ready to remote debug at port 5005.

Now we proceed to set the breakpoint then click on Run and Debug then click on Attach to remote Program

So we have successfully debug. The next steps for debugging will be the same as the first part of the article.

image.png

Thanks for reading my writing. If there are any mistakes, you can let me know so I can make the article better.

 

Share the news now

Source : Viblo