Build Java Web Apps with VS Code

This 5 minutes tutorial shows you how to create a simple Java web application with Visual Studio Code. You'll learn how to run and debug it locally, and prepare for running it in the cloud.

Scenario

A simple Spring Boot Getting Started web app

Greeting from Java

Before you begin

Before running and deploying this sample, you must have JDK and Maven on your local development environment. If not, 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/microsoft/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, you can open the cloned repository in VS Code.

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

Clone Spring Repository

After cloning the repo, navigate to the complete folder that contains the project which is ready to run. Open any of the Java files in the repository. If you haven't got the Java related extensions installed with your VS Code, you would be recommended to install the Java extension pack. Just follow the instruction and install the Java Extension Pack from Microsoft. Reload VS Code after the installation.

Install Java Extensions

Once you have the Extension Pack installed, it will automatically build the project for you, and then you can run it within VS Code by pressing F5. You can learn more about how VS Code launch your app at Launch Configurations.

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.

Simply click the Restart button on the top of the editor to reload the app and see result by reloading the browser.

Restart Application

Debug the application

Set a breakpoint 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, please read Java Debugging.

Congratulations, now you have your first Spring Boot web app running locally! Now let's try host it on cloud.

Next steps