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
- Go to the Run and debug panel on the left navigation menu, then click create a launch.json file.
![Screenshot 2023-03-08 at 15.40.20.png 3446](https://files.readme.io/ef577a3-Screenshot_2023-03-08_at_15.40.20.png)
- 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.
![Screenshot 2023-03-08 at 16.05.31.png 3450](https://files.readme.io/de31de0-Screenshot_2023-03-08_at_16.05.31.png)
- Open a terminal and start the JVM process with debugging enabled.
![Screenshot 2023-03-08 at 16.13.26.png 3452](https://files.readme.io/8deaefc-Screenshot_2023-03-08_at_16.13.26.png)
-
Click the green play button next to the configuration name. You will notice the layout of VS Code changing for debugging.
-
Add a breakpoint.
![Screenshot 2023-03-08 at 16.15.27.png 3454](https://files.readme.io/c92311f-Screenshot_2023-03-08_at_16.15.27.png)
- Access the endpoint of the application to generate a request, and you will notice the breakpoint being hit.
![Screenshot 2023-03-08 at 16.17.59.png 3456](https://files.readme.io/3725080-Screenshot_2023-03-08_at_16.17.59.png)
Congratulations! You have now got a completely working environment for Remote Development in the cloud.
Updated 8 months ago