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