Build Java Web Apps with VS Code
This tutorial shows you how to create a simple Java web application with Visual Studio Code. You'll learn how to run, debug, and edit the Java web app locally.
A simple Spring Boot Getting Started web app
Before you begin
Before running and deploying this sample, you must have the Java SE Development Kit (JDK) and Apache Maven build tools on your local development environment. If you don't, please install them.
Download and install the Java SE Development Kit (JDK), version 8:
JAVA_HOMEenvironment variable must be set to the install location of the JDK to complete this tutorial.
Download Apache Maven version 3 or greater:
Install Apache Maven for your local development environment:
Download and test the Spring Boot app
Clone the Spring Boot Getting Started sample project to your local machine. You can clone a Git repository with the Git: Clone command in the Command Palette (⇧⌘P (Windows, Linux Ctrl+Shift+P)). Paste
https://github.com/spring-guides/gs-spring-boot.git as the URL of the remote repository and then decide the parent directory under which to put the local repository. After that, open the cloned repository in VS Code by navigating to the repository folder and typing
From within VS Code, navigate to the
complete folder that contains the project which is ready to run. Open any of the Java files in the repository (for example
complete\src\main\java\hello\Application.java). If you don't have the Java language extensions installed for VS Code, you will be prompted to install the Microsoft Java Extension Pack. Follow the instructions and reload VS Code after the installation.
Once you have the Java Extension Pack installed, it will automatically build the project for you (this may take several minutes). You can run the application within VS Code by pressing F5 and selecting the Java environment. The Java Debug extension will generate a debugging configuration file
launch.json for you under a
.vscode folder in your project. You can see build progress in the VS Code Status Bar and when everything is finished, the final active debug configuration is displayed.
You can learn more about how VS Code launches your application in Debugging Launch Configurations. Press F5 again to launch the debugger.
Test the web app by browsing to
http://localhost:8080 using a web browser. You should see the following message displayed: "Greetings from Spring Boot!".
Make a change
Let's now edit
HelloController.java to change "Greetings from Spring Boot!" to something else like "Hello World". VS Code provide a great editing experience for Java, check out Editing and Navigating Code to learn about VS Code's editing and code navigation features.
Click the Restart button on the top of the editor to relaunch the app and see result by reloading the browser.
Debug the application
Set a breakpoint (F9) in the application source code, and reload your browser to hit the breakpoint.
If you would like to learn more about debugging Java with VS Code, you can read Java Debugging.
Congratulations, you have your first Spring Boot web app running locally! Read on to learn how to host it in the cloud.
- If you'd like to learn how to deploy your web application, checkout out the Deploy a Java Application to Azure Web App tutorial where we show how to run your web app in the cloud.
- To see how you can containerize the web app and deploy to the cloud as a Docker container, check out Java Container Tutorial
- To learn more about Java Debugging features, see Java Debugging Tutorial