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.

Scenario

A simple Spring Boot Getting Started web app

Greeting from Java

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:

Download JDK

Note: The JAVA_HOME environment variable must be set to the install location of the JDK to complete this tutorial.

Download Apache Maven version 3 or greater:

Download Apache Maven

Install Apache Maven for your local development environment:

Install Apache Maven

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 code ..

Note: You can install Visual Studio Code from https://code.visualstudio.com and Git from https://git-scm.com.

Clone Spring Repository

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.

Install Java Extensions

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.

debug configuration in the Status Bar

You can learn more about how VS Code launches your application in Debugging Launch Configurations. Press F5 again to launch the debugger.

Run Spring Boot

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!".

Greeting from Spring

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.

Restart Application

Debug the application

Set a breakpoint (F9) in the application source code, and reload your browser to hit the breakpoint.

Debug Application

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.

Next steps