Getting Started with Java in VS Code

This tutorial shows you how to write and run Hello World program in Java with Visual Studio Code. It also covers a few advanced features, which you can explore by reading other documents in this section.

For an overview of the features available for Java in VS Code, see Java Language Overview

If you run into any issues when following this tutorial, you can contact us by clicking the Report an issue button below.

Report an issue

If you need to install VS Code, download it here.

Supported Java versions

Supported versions for running VS Code for Java and supported versions for your projects are two seperate concepts. To run VS Code for Java, Java SE 11 or above version is required; for projects, VS Code for Java supports projects with version 1.5 or above. For more details, refer to Configure JDK.

Setting up Visual Studio Code for Java Development

To help you set up quickly, here are the recommendations based on your experience with VS Code:

New to VS Code: Use the Installer of Visual Studio Code for Java developers.

Use the Installer of Visual Studio Code for Java developers. Existing VS Code user wanting to add Java support: Install the Java Extension Pack.

Installer of Visual Studio Code for Java developers

There are special installers of Visual Studio Code for Java developers, which include all the necessary dependencies and extensions for Java development in Visual Studio Code:

Note: The installers are only available for Windows and macOS. For other operating systems, you will need to manually install a JDK, VS Code, and Java extensions.

The package can be used as a clean install, or you can also use it to update an existing development environment to add Java or VS Code. After you've downloaded and opened it, the installer automatically tries to detect a JDK, VS Code, and essential Java extensions. During install, it downloads the stable versions of those tools from trusted online sources then installs and configures them.

Alternatively, you can follow instructions below to install JDK or extensions seperately per your preference.

Installing a Java Development Kit (JDK)

Your development environment must have a Java SE Development Kit (JDK) installed. If it doesn't, you can download and install a JDK from one of these sources:

Configuring your development environment to use a JDK

Your development environment needs to know where the JDK is located. A common way to do this is setting the value of the JAVA_HOME system environment variable to the install location of the JDK, for example, C:\Program Files\Java\jdk-13.0.2 . Or if you want to configure only VS Code to use the JDK, use the java.home setting in VS Code's User or Workspace settings.

Installing extensions

You can add Java support to VS Code by installing the popular Microsoft Java Extension Pack, which includes these extensions:

If JDK is not installed, the Java Extension Pack provides links to download.

Install the Java Extension Pack

You can also select which extensions you would like to install separately. The Extension Guide is provided to help you choose. You can launch the guide with the Java: Extension Guide command.

For this tutorial, the only required extensions are:

Settings for the JDK

To access various settings for using the JDK, bring up the Command Palette (⇧⌘P (Windows, Linux Ctrl+Shift+P)) and use the command Java: Configure Java Runtime.

The Java Extension Pack, also provides a Quick Start guide and tips for code editing and debugging. It also has a FAQ that answers some frequently asked questions. Use the command Java: Getting Started from the Command Palette (⇧⌘P (Windows, Linux Ctrl+Shift+P)).

Creating a source code file

Create a folder for your Java program and open the folder with VS Code. Then in VS Code, create a new file and save it with the name Hello.java . When you open that file, the Java Language Server automatically starts loading, and you should see a loading icon on the right side of the Status Bar. After it finishes loading, you will see a thumbs-up icon.

Note: If you open a Java file in VS Code without opening its folder, the Java Language Server might not work properly.

VS Code will also try to figure out the correct package for the new type and fill the new file from a template. See Create new file.

You can also create a Java project using the Java: Create Java Project command. Bring up the Command Palette (⇧⌘P (Windows, Linux Ctrl+Shift+P)) and then type java to search for this command. After selecting the command, you will be prompted for the location and name of the project. You can also choose your build tool from this command.

Visual Studio Code also supports more complex Java projects, see Project Management.

Editing source code

You can use code snippets to scaffold your classes and methods. VS Code also provides IntelliSense for code completion, and various refactor methods.

To learn more about editing Java, see Java Editing.

Running and debugging your program

To run and debug Java code, set a breakpoint, then either press F5 on your keyboard or use the Run > Start Debugging menu item. You can also use the Run|Debug CodeLens options in the editor. After the code compiles, you can see all your variables and threads in the Run view.

The debugger also supports advanced features such as Hot Code replacement and conditional breakpoints.

For more information, see Java Debugging.

The editor also has much more capability for your Java workload.