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:
java -jar app.jar
To enable remote debugging you need to pass these additional arguments:
java -Xdebug -Xrunjdwp:server=y,transport=dt_socket,address=8000,suspend=n -jar app.jar
-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_socket’ or ‘shmem’. ‘dt_socket’ instructs to socket interface. ‘shmem’ will instruct application and debugger interace 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. ‘y’ means application will be suspended until any remote debugger is connected to the application. ‘n’ means application will not be suspened 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:
(1). Click on the Debug menu icon
Fig: Click on ‘Debug’ menu icon
(2). Click on ‘Debug Configurations…’ menu item
(3). In the left panel select ‘Remote Java Application’
Fig: select ‘Remote Java Application’
(4). Press the ‘New’ button
Fig: Click on ‘New’ button
(5). Now you need enter project and connectivity details:
a. In the ‘Name’ field you can enter any name. Example: myapp-remotedebugging
b. In the ‘Project’ field select your applications source code that you want to debug.
c. In the ‘Host’ field enter the hostname in 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’
Fig: Enter project & connectivity details
(6). 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.
Don’t keep remote debugging JVM arguments ON always, as it has following downsides:
a. Remote debugging mode disables several optimizations that JVM does to application to optimize the performance. All those optimizations will be lost.
b. Remote debugging opens up a port. It’s a security risk, as anyone who can hit the server can initiate remote debugging.