Overview

4 Debugging apps remotely

This chapter explains how to debug a Java application when the problem appears only in a remote environment and cannot be reproduced locally. It begins with a real-world style example in which an app behaves differently across environments because of deployment and infrastructure differences, such as configuration, operating system, permissions, resources, or even limited entropy in virtualized systems. The main idea is that when “it works on my machine” is no longer useful, remote debugging can help you inspect the running application where the issue actually occurs.

The chapter then clarifies what remote debugging is: using familiar debugging tools and techniques on an application that is running somewhere else. It emphasizes that this is practical in development or UAT environments, but should not be used in production because the debugging agent can slow the app down, require open network ports, interfere with live requests, or expose sensitive data. It also highlights an important prerequisite: you need the debugger agent attached to the Java process, typically with the JDWP agent configuration, so the debugger can connect over a network port and control execution.

Finally, the chapter walks through a case study where a backend endpoint returns HTTP 200 but the expected data is null in UAT. After starting the app with a remote debug agent, connecting from the IDE, and ensuring the source code matches the deployed version, the developer sets a breakpoint and sends a request through a client tool. The paused execution reveals that an exception is thrown because a nullable database column was mapped to a primitive type, and the stack trace was not being logged in a useful way. The investigation shows how remote debugging can quickly uncover hidden environment-specific bugs and points to the need for better code handling and review practices.

Remotely debugging an app. The developer can run the debugger tool locally but connect it to an app instance running in a different environment. This approach allows the developer to investigate problems that only occur in specific environments.
When building a real-world app, developers often use multiple environments. First, they build the app in development (dev) environments. Then, once a feature or a solution is ready, they present it to users (or stakeholders of the app) using a user acceptance test (UAT) environment. Finally, after the stakeholders confirm that the implementation works, they install it in a production (prod) environment.
Developers implement the app using the dev and UAT environments. It’s OK to debug apps in these environments. But remember, never debug apps in the prod environment, as this can affect the app’s execution, interfere with users’ actions, and even expose sensitive data, creating a security vulnerability.
The scenario you have to investigate. The /api/product/total/costs endpoint should return the total costs from the database. Instead, when a request is sent to the endpoint, the app behaves oddly. The HTTP status is 200 OK, but the total costs, which you expected to be a list of values, comes back null.
When debugging an app locally, the IDE attaches the debugger. But when running an app in a remote environment, you must attach a debugger agent at the app start yourself.
When you run the command to start the app, you can see that the app begins executing. At the same time, you can see that the debugging agent printed a line showing that it is listening for a debugger to attach on the configured port 5005.
You can use an IDE to configure the debugger to attach to an already running app in a particular environment, as long as the app has a debugger agent attached to it. In IntelliJ IDEA Community, you need to create a new running configuration to tell the debugger to attach to an already running app. You can add a new run configuration by selecting Edit Configurations.
Once you’ve selected Edit Configurations, you can add a new configuration. First, click the plus icon, and then Add New Configuration.
Since we want to attach the debugger to an app running in a remote environment, select the Remote JVM Debug configuration type.
Give a name to the new configuration you add and specify the address of the environment and port you configured the debugger agent to listen on (here, port 5005 when starting the app).
The debugger tool that runs on the developer’s computer connects to the debugger agent on port 5005. The debugger agent allows the debugger tool to control the app. The app also opens a port, but this port is for its clients (the browser, in the case of a web app).
You can now run the debugger using the newly added configuration. Click the small bug icon to start the debugger.
The developer needs to make sure they have the same version of source code as the one used to generate the app’s executable run in the remote environment. Otherwise, the debugger's actions could become inconsistent with the code the developer investigates, which would create more confusion than it would help the developer understand the app’s behavior.
Just as when debugging an app locally, you can add breakpoints and use navigation operations. Add a new breakpoint on line 23 in the ProductService class.
When using Postman to send the request, the response doesn’t come back immediately. Instead, Postman waits indefinitely for the response because the app paused the execution on the line you marked with a breakpoint.
The IDE shows that the debugger did indeed pause the execution on the line you marked with a breakpoint. Thus, you can use navigation operations to continue your investigation.
The step over operation shows that the app throws an exception. Now you have an idea of what the problem is and can decide how to solve it.

Summary

  • Sometimes the specific unexpected behavior of a running app happens only in certain environments in which the app executes. When this happens, debugging becomes more challenging.
  • You can use a debugger with a Java app that executes in a remote environment with some conditions:
    • The app should be started with a debugger agent attached.
    • The network configuration should allow communication between the debugger tool and the debugger agent attached to the app in the remote environment.
  • Remote debugging allows you to use the same debugging techniques as local debugging by attaching to a process that runs in a remote environment.
  • Before debugging an app running in a remote environment, make sure the debugger uses a copy of the same source code that created the app you are investigating. If you don’t have the exact source code and changes are made in the parts of the app involved in your investigation, the debugger may behave oddly, and your remote investigation will become more challenging than helpful.

FAQ

What is remote debugging in Java applications?Remote debugging is the process of using your debugger tool to inspect and control a Java application that is running in a different environment, rather than on your local machine. It lets you apply the same debugging techniques you use locally to a remote app instance.
When should you use remote debugging?Use remote debugging when a problem cannot be reproduced on your local computer but does occur in another environment such as dev, UAT, or a remote test system. It is especially useful when environment-specific differences affect behavior.
Why can an application behave differently in remote environments?Applications can behave differently because environments may have different data, operating systems, deployment setups, permissions, or resource limits such as memory and CPU. These differences can change how the app runs even if the code is the same.
Why is remote debugging useful for investigating performance problems?Remote debugging helps you inspect the exact environment where the slowdown occurs. This is important when the issue is caused by something environment-specific, such as low entropy, different infrastructure, or deployment conditions that do not exist locally.
Why should you avoid remote debugging in production?You should avoid remote debugging in production because it can slow down the application, require opening network ports, interfere with user activity, and expose sensitive data. It can also block execution and force a restart.
What is the purpose of the Java debug agent?The debug agent is a piece of software attached to the Java application at startup so a debugger can connect to it remotely. It listens on a configured port and allows the debugger to pause execution, step through code, and inspect variables.
What does the -agentlib:jdwp option do?The -agentlib:jdwp option enables Java debugging wire protocol support for the application. It configures the app to accept a debugger connection and specifies how the debugger and the agent communicate.
Why is the debugger agent usually configured with a port like 5005?The port is where the debugger tool connects to communicate with the running application. The chosen port must be available and allowed by the network, and it must not conflict with the app’s normal client-facing port.
What should you verify before attaching a debugger to a remote app?You should verify that network communication is allowed, the correct port is open, and you have the same source code version that was used to build the running application. Otherwise, debugging can become confusing or misleading.
What kind of issue was found in the chapter’s remote debugging example?The debugger revealed that the app threw an exception because a primitive type was being used for a database column that could contain null. Since primitives cannot hold null, the app failed instead of returning the expected data.