Writing Java with Visual Studio Code

This tutorial shows you how to write and run a simple 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.

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

Install Visual Studio Code for Java

Before you begin

Before going through this tutorial, you must have the Java SE Development Kit (JDK) on your local development environment. If you don't have them installed, you can do so now.

Download and install the Java SE Development Kit (JDK). Java support on Visual Studio Code works with all major Java versions up to 13, see a few JDK distributions you can choose from:

Note: After installing the JDK, you would need to configure your environment for Java development. The most common way is to set JAVA_HOME environment variable to the install location of the JDK while you can also use java.home setting in Visual Studio Code settings (workspace or user settings) to configure it just for the editor.

To help you get started quickly, we created a special Installer of Visual Studio Code for Java developers.

Download Visual Studio Code Java Pack Installer

Note: The installer is currently only available for Windows. For other OS, please install those components (JDK, VS Code and Java extensions) individually. We're working on the macOS version, please stay tuned.

The package can be used as a clean install or an update for an existing development environment to add Java or Visual Studio Code. Once downloaded and opened, it automatically detects if you have the fundamental components in your local development environment, including the JDK, Visual Studio Code, and essential Java extensions. During install, it downloads the stable versions of those tools from trusted online sources, then installs and configures them on your system.

Detect Environment

Alternatively, you can also add Java language support to VS Code by installing the popular Java extensions by yourself.

Note: You can install Visual Studio Code from https://code.visualstudio.com.

There is the Microsoft Java Extension Pack, which contains these popular extensions:

  1. Language Support for Java(TM) by Red Hat
  2. Debugger for Java
  3. Java Test Runner
  4. Maven for Java
  5. Java Dependency Viewer

Install the Java Extension Pack

You can also select which extensions you would like to install separately. For this tutorial, only Language Support for Java(TM) by Red Hat and Debugger for Java are needed.

Java Extension Pack also detects your environment to see whether a JDK is present. If not, it will provide you links to download reliable JDK at your choice.

Acquire JDK

This page also allows you to configure your JDK anytime you want in various levels. You can get it by using the command Java: Configure Java Runtime.

With the Java Extension Pack, we also provide an easy way for you to start Java with VS Code by using the command Java: Getting Started from the Command Palette (⇧⌘P (Windows, Linux Ctrl+Shift+P)). This command opens a Quick Start guide as well as tips for code editing and debugging. It also has a FAQ to answer some of the most asked questions.

Java Getting Started

Create the program

Create a folder for your Java program and open the folder with Visual Studio Code. Within Visual Studio Code, you can then create a new file Hello.java. When you open that file, the Java Language Server will automatically be activated as you can see a loading icon on the right of the Status bar. When it finishes loading, you will see a thumbs-up icon instead.

Note: The Java Language Server might not work properly if you open a standalone Java file in Visual Studio Code without opening its folder.

You can also create an Eclipse style 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.

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

Editing code

In Visual Studio Code, you can easily use code snippets to scaffold your class and method. VS Code also provides IntelliSense for code completion.

You can also use various refactor methods within the editor. To learn more about Java code editing in Visual Studio Code, see Java Editing.

Running and debugging your program

It is easy to run and debug Java in Visual Studio Code. You can either press F5 on your keyboard or select the Start Debugging command in the Run menu. Alternatively, you can also use the Run|Debug options on a CodeLens. Set a breakpoint and 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.

More features

Now you've seen how easy it is to write a simple Java program with Visual Studio Code. The editor has much more capability to offer for your Java workload.

To learn how to work with Java Web App in VS Code, read Java Web App.