1. Install the necessary extensions
-
Java Extension Pack: Includes all required Java extensions, such as
Language Support for Java(TM) by Red Hat
andDebugger for Java
。 -
Spring Boot Extension Pack:include
Spring Boot Extension
andSpring Boot Dashboard
。
2. Configure the environment
Make sure the following software is installed:
- Java Development Kit (JDK): It is recommended to use JDK 11 or higher.
- Maven: Used to build and manage Spring Boot projects.
- Spring Boot CLI (optional): Used to quickly create Spring Boot projects.
3. Create or import Spring Boot project
Create a new project using Spring Initializr
-
Open the command panel(according to
Ctrl+Shift+P
orCmd+Shift+P
)。 - Input
Spring Initializr: Generate a Maven Project
And select this option. - Follow the prompts to select the configuration of the project, such as Group, Artifact, Dependencies, etc.
- Select the project generation location and click "Generate".
- VSCode will prompt you to open the generated project.
Import existing projects
- Drag the project folder directly into the VSCode window.
- VSCode will automatically detect and recommend that you install the required extensions (if not already installed).
4. Configure VSCode
Configure the debugging environment
-
Open the debug view(Click on the bug icon on the sidebar or press
Ctrl+Shift+D
)。 - Click
create a file
, and then selectJava
。 - VSCode will generate a
Files, usually located in
.vscode
In the folder, the content is as follows:Will{ "version": "0.2.0", "configurations": [ { "type": "java", "name": "Debug (Launch) - Current File", "request": "launch", "mainClass": "${file}" }, { "type": "java", "name": "Debug (Attach)", "request": "attach", "hostName": "localhost", "port": 5005 }, { "type": "java", "name": "Debug (Launch) - MyApp", "request": "launch", "mainClass": "", "projectName": "my-app" } ] }
mainClass
andprojectName
Modify to the actual value of your project.
5. Run and debug projects
Run via command line
-
Open the terminal(according to
Ctrl+
orCmd+
)。 - Navigate to the project root directory.
- Run
mvn spring-boot:run
Command to start the Spring Boot application.
Run via Spring Boot Dashboard
- Click the Spring Boot Dashboard on the sidebar.
- Find your project in Spring Boot Dashboard.
- Click the play button to run the project.
Run in debug mode
- Set breakpoint: Click on the left side of the code line number to add a breakpoint.
- Open the debug view (press
Ctrl+Shift+D
)。 - Select the previously configured
Debug (Launch) - MyApp
Configuration. - Click the green Start button to start debugging.
6. Use Spring Boot Actuator (optional)
If your project contains Spring Boot Actuator, you can access the endpoint (such as/actuator/health
) to monitor and manage applications.
7. Configure task automation (optional)
You can use VSCode's task system to automate the build and run steps:
- Create
.vscode/
document. - Add the following configuration:
{ "version": "2.0.0", "tasks": [ { "label": "Run Spring Boot", "type": "shell", "command": "mvn spring-boot:run", "group": "build", "problemMatcher": [], "detail": "Runs the Spring Boot application" } ] }
- You can run this task through the Task Panel or shortcut keys.
8. Summary
The above is a complete guide on how to configure and run a Java Spring Boot project in VSCode. Installing the necessary extensions, configuring the debugging environment, and running and debugging Spring Boot applications through the command line or VSCode built-in tools can improve your development efficiency.
This is the end of this article about running Java SpringBoot projects in VS Code. For more related contents of running SpringBoot projects in VS Code, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!