Using Java 21 with VS Code: A Lightweight Workflow

Java 21 brings modern features that simplify development and improve performance. Whether you’re a student, hobbyist, or a developer tired of heavy IDEs like Eclipse or IntelliJ, Visual Studio Code (VS Code) offers a fast, lightweight, and highly productive setup for Java development.

In this guide, you’ll learn how to set up Java 21 with VS Code, write and run your first Java app, and use advanced features like virtual threads, all with minimal configuration.

๐Ÿš€ Why Use VS Code for Java?

While Java has traditionally been used with heavyweight IDEs, VS Code is:

  • ๐Ÿ’จ Fast and lightweight โ€” consumes fewer system resources
  • ๐Ÿ”Œ Highly extensible โ€” thousands of extensions available
  • ๐ŸŽฏ Focused โ€” only install what you need
  • ๐Ÿงช Great for experimentation โ€” ideal for trying Java 21 features

๐Ÿ› ๏ธ Step 1: Install Java 21

You need Java 21 installed on your system. Java 21 is a Long-Term Support (LTS) release, making it ideal for new projects.

๐Ÿ‘‰ Download Options:

๐Ÿ” To verify installation:


java -version

Expected output:


java 21.0.x ...

Also check:


javac -version

๐Ÿงฉ Step 2: Install VS Code and Java Extensions

  1. Download VS Code:
    https://code.visualstudio.com
  2. Install Java Extension Pack:

๐Ÿ“ฆ This includes:

  • Language support for Javaโ„ข
  • Debugger for Java
  • Maven & Gradle support
  • Java Test Runner
  • IntelliSense and formatting
Java 21 with VS Code

๐Ÿ“ Step 3: Create a Java Project

You can start small with a single .java file or set up a Maven/Gradle project.

๐Ÿ“„ Option A: Simple Java file

Open a terminal:


mkdir HelloJava21
cd HelloJava21
code .

Inside VS Code, create a file Hello.java:


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

๐Ÿ”จ Option B: Use Maven (recommended for larger projects)

  • Open Command Palette (Ctrl + Shift + P)
  • Run: Java: Create Java Project
  • Choose Maven
  • Fill in details like Group ID and Artifact ID
  • VS Code sets up everything for you

โ–ถ๏ธ Step 4: Run Your Java Code

For single files:

  • Click the Run button that appears above main() in the editor
  • OR use the terminal:

javac Hello.java
java Hello

If you’re using Maven:


mvn compile
mvn exec:java

๐Ÿง  Step 5: Explore Java 21 Features

Now you’re ready to use powerful features in Java 21!

โœ… Record Patterns


record Person(String name, int age) {}

static void greet(Object obj) {
    if (obj instanceof Person(String name, int age)) {
        System.out.println("Hi " + name + ", age " + age);
    }
}

โœ… Virtual Threads (Project Loom)


Runnable task = () -> System.out.println("Running on " + Thread.currentThread());
Thread.startVirtualThread(task);

โœ… Sequenced Collections


List names = new ArrayList<>();
names.add("Alice");
names.add("Bob");

for (String name : names.reversed()) {
    System.out.println(name);
}

Youโ€™ll get full IntelliSense, compile-time checking, and tooltips right in VS Code.

๐Ÿž Debugging in VS Code

Debugging in VS Code is straightforward:

  1. Add a breakpoint (click to the left of the line number)
  2. Click the Run and Debug icon on the sidebar
  3. Select Java
  4. Use controls to step through the code, watch variables, and evaluate expressions

๐Ÿงผ Formatting & Auto Compile

You can format code automatically:

  • File > Preferences > Settings > Java > Format
  • Add this to .vscode/settings.json:

{
  "editor.formatOnSave": true,
  "java.format.settings.profile": "GoogleStyle"
}

You can also enable auto-build so your code compiles as you save.

๐Ÿงฐ Integrating Build Tools

VS Code supports:

  • Maven (via pom.xml)
  • Gradle (via build.gradle)

Features include:

  • Lifecycle commands (clean, compile, test)
  • Dependency management
  • Integrated terminal

Youโ€™ll see a Maven or Gradle side panel for easy navigation.

๐Ÿ Final Thoughts

VS Code + Java 21 gives you the best of both worlds:

  • Lightweight and fast tooling
  • Powerful new language features
  • Simpler setup than traditional IDEs

Whether you’re building REST APIs, microservices, or learning the language, this setup helps you move fast and stay productive.