Remote debugging for Java applications
Remote testing is often necessary to fix problems in production. In this article, Ram Lakshmanan explains how developers can do remote debugging to production servers without much trouble at all.
Few problems might happen only on test or production servers. It may not be reproducible in your local machine. In those circumstances, you want to connect your IDE to the remote test (or production) servers and do remote debugging.
Java applications can be remotely debugged by following these two simple steps:
- Pass remote debugging arguments to JVM.
- Configure IDE.
Let’s review these two steps in this article.
Step 1: Pass remote debugging arguments to JVM
Typically, you would launch your Java application like this:
<<start:code>> java -jar app.jar <<end:code>>
To enable remote debugging you need to pass these additional arguments:
<<start:code>> java -Xdebug -Xrunjdwp:server=y,transport=dt_socket,address=8000,suspend=n -jar app.jar <<end:code>>
-Xdebug: Enables JVM to do remote debugging.
-Xrunjdwp: Specifies the connectivity details:
transport: Configures transport between application and debugger. It can have 2 values:
dt_socketinstructs to socket interface.
shmemwill instruct application and debugger interface through shared memory region, which is useful only when both application and debugger are running on same machine.
address: Port which will be opened by the application for remote debugging.
suspend: It can have two values.
ymeans application will be suspended until any remote debugger is connected to the application.
nmeans application will not be suspended even if no remote debugger is connected to the application.
Step 2: Configure IDE
Below are the steps to configure Eclipse IDE to connect to your remote application:
- Click on the Debug menu icon.
- Click on ‘Debug Configurations…’ menu item.
- In the left panel select ‘Remote Java Application’.
- Press the ‘New’ button.
- Now you need enter project and connectivity details:
a. In the ‘Name’ field you can enter any name. Example:
b. In the ‘Project’ field select your applications source code that you want to debug.
c. In the ‘Host’ field enter the
hostnamein which your application is running.
d. In the ‘Port’ field enter the port number that you specified in step #1. As per this example it would be ‘8000’.
- After entering all these details click on the ‘Debug’ button. That’s it. Now you are all set for doing remote debugging.
Wish you ‘Happy Debugging’. Hopefully, it’s not that painful.
Warning: Don’t keep remote debugging JVM arguments ON always, as it has following downsides:
- Remote debugging mode disables several optimizations that JVM does to application to optimize the performance. All those optimizations will be lost.
- Remote debugging opens up a port. It’s a security risk, as anyone who can hit the server can initiate remote debugging.