Kill bugs with this helpful trick

Remote debugging for Java applications

Ram Lakshmanan
© Shutterstock /Krisana Antharith

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:

  1. Pass remote debugging arguments to JVM.
  2. 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 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. y means application will be suspended until any remote debugger is connected to the application. n means application will not be suspended even if no remote debugger is connected to the application.

SEE ALSO: What happens behind the scenes for finalize() method?

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.

    remote debugging java 1

    Figure 1: Click on ‘Debug’ menu icon

  2. Click on ‘Debug Configurations…’ menu item.
  3. In the left panel select ‘Remote Java Application’.

    remote debugging java 2

    Figure 2: Select ‘Remote Java Application’

  4. Press the ‘New’ button.

    remote debugging java 3

    Figure 3: 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’.

    remote debugging java 4

    Figure 4: 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.

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.

Ram Lakshmanan

Every single day, millions & millions of people in North America—bank, travel, and commerce—use the applications that Ram Lakshmanan has architected. Ram is an acclaimed speaker in major conferences on scalability, availability, and performance topics. Recently, he has founded a startup, which specializes in troubleshooting performance problems.

Inline Feedbacks
View all comments