Ukraine needs your help!

Support

How to Debug Spring Boot in VS Code?

Tools, Java, Kotlin

This is a short note on how to configure Visual Studio Code to be able to debug your Spring Boot application using Remote Debug.

Prerequisites

First of all, install Debug for Java extension. In case you are using Kotlin, install the Kotlin extension too. After, to be able to debug your application we have to run the application using spring-boot runner.

Maven

./mvnw spring-boot:run -Dspring-boot.run.jvmArguments="-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5005"

Gradle

./gradlew bootRun --debug-jvm

Here, we are using Maven, Gradle wrapper (gradlew, mvnw).

This command allows us to run Spring Boot application in debug mode using transport port 5005.

Debug Task

Let’s provide a comfortable way to use the command above through VS Code Tasks.

Add the following lines to .vscode/tasks.json:

{
  "tasks": [
    {
      "label": "Maven Debug",
      "type": "shell",
      "command": "./mvnw spring-boot:run -Dspring-boot.run.jvmArguments=\"-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5005\"",
      "group": "build"
    },
		{
      "label": "Gradle Debug",
      "type": "shell",
      "command": "./gradlew bootRun --debug-jvm",
      "group": "build"
    }
  ]
}

After, save this file and press [Ctrl + P] enter "Task" and press [Space]:

VS Code New Task

Now, we could easily run any of these tasks. Let’s choose "Gradle Debug" and press [Enter]:

VS Code New Task Result

As we can see debug port is available 5005.

Run and Debug

On the left side of VS Code choose the "Run and Debug" item and click "create a launch.json file"

VS Code -> launch.json

{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "Kotlin:Debug",
      "type": "kotlin",
      "request": "attach",
      "projectRoot": "${workspaceFolder}",
      "timeout": 30000,
      "hostName": "localhost",
      "port": 5005
    },
		{
      "name": "Java:Debug",
      "type": "java",
      "request": "attach",
      "timeout": 30000,
      "hostName": "localhost",
      "port": 5005
    }
  ]
}

After, you will see these executors in the "Run and Debug" section:

VS Code -> Debug runner

Select and Run.

VS Code -> Debug View

Comments

Alex BarchukBlog about programming 🚀bcode.dev © 2022