Java Remote Debugging. Debug Java applications running on another computer.

Tram Ho

This article is for those of you who are using IntelliJ

1. Make a problem

In some cases, your application runs on the local machine, but crashes on another environment (test, production). Normally I would do the following:

  • Add Log to suspicious areas to check, then check log (common way)
  • Get the broken branch code, try to run it locally to check (Hard to do, can be restricted with network policy, environment variables can be different, sometimes not 100% accurate).

I will give one more way ( Remote Debugging in IntelliJ ) to help us debug the running application.

2. Solution

2.1 Run the application in debug mode

I have created a simple service When running it will export 1 api /test/get

Step 1 clone the project. Go to the directory, build jar file

The result is the Jar file in the directory

You can run the jar file on another machine on the internet, lan or localhost itself. To enable debugging, we will have to add some options to run it. with java 5-8

JDK 9 and later

Port 5005 can be replaced with another port

In this case I use java 11. my command will be

The application is started and listens to the socker on port 5005

image.png

2.2 Run IntelliJ to debug the application

Open the project on IntelliJ, select Edit Configuration

image.png

Add new configuration, select Remote JVM Debug

image.png

Configure Host (Server running the application) Port 5005 above -> Apply

image.png

After starting the application, if successful, there will be a message like this (34,124,158,186 is the server I run the application on).

Connected to the target VM, address: '34.124.158.186:5005', transport: 'socket' image.png

Set a break point and call the test api to the service you are running. (my will call to http://34.124.158.1886:8080/test/get ) Depending on the server you publish, the url will be different

See Break Point is hit.

image.png

2.3 Source code

All source code I have shared on github .

3. Conclusion

I give one more way for you to solve when you have a problem, depending on the problem, you will choose the appropriate way. There is no best way. If you need to discuss, please comment below. Thanks for reading my post.

4. References

Share the news now

Source : Viblo