Java needs a JDK (Java Development Kit) and a little project structure. This guide covers running, debugging and building Java in VS Code.

Install a JDK

  • All platforms: install a current LTS JDK such as Eclipse Temurin (adoptium.net) or Microsoft Build of OpenJDK.
  • macOS: brew install temurin.
  • Linux: sudo apt install openjdk-21-jdk.

Verify: java -version and javac -version

VS Code

Install the Extension Pack for Java by Microsoft. It bundles language support, the debugger, a test runner and Maven/Gradle integration.

Write, compile and run

Create Main.java:

class Main {
    public static void main(String[] args) {
        System.out.println("Hello, Java!");
    }
}

From the terminal:

javac Main.java
java Main

With the extension installed you can also just press the ▶ Run button above main.

Debug it

Set a breakpoint and click "Debug" above the main method (or press F5). The debugger lets you inspect objects, evaluate expressions and step through call stacks.

Build with Maven or Gradle

Real projects use a build tool. With Maven:

mvn package
java -jar target/app-1.0.jar

mvn package compiles, runs tests and produces a JAR. Gradle (gradle build) does the same with a different config style. Both manage dependencies for you so you never download library JARs by hand.