Debug Java

Pre-requisites

By default, the JVM doesn't enable debugging. This is because debugging creates additional overhead inside the JVM. It can also be a security concern for applications that are publicly accessible.

Before we can attach a debugger, we must first configure the JVM to allow debugging. We'll do this by setting a command line argument for the JVM:

-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5005

Once the connection is established, we can proceed to setting up debugging.


Steps

  1. Go to the Run and debug panel on the left navigation menu, then click create a launch.json file.
3446
  1. Add a Attach: Java configuration (or ensure you already have it) under configurations. Update the port if needed.
...
"configurations": [
  {
    "type": "java",
    "name": "Debug remote",
    "request": "attach",
    "hostName": "localhost",
    "port": "5005"
  },
]
...

You will be able to see the new configuration in the top of the screen.

3450
  1. Open a terminal and start the JVM process with debugging enabled.
3452
  1. Click the green play button next to the configuration name. You will notice the layout of VS Code changing for debugging.

  2. Add a breakpoint.

3454
  1. Access the endpoint of the application to generate a request, and you will notice the breakpoint being hit.
3456

Congratulations! You have now got a completely working environment for Remote Development in the cloud.