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:
1 2 3 |
java -version |
Expected output:
1 2 3 |
java 21.0.x ... |
Also check:
1 2 3 |
javac -version |
๐งฉ Step 2: Install VS Code and Java Extensions
- Download VS Code:
https://code.visualstudio.com - Install Java Extension Pack:
- Open VS Code
- Go to Extensions (
Ctrl + Shift + X
) - Search for Java Extension Pack
- Click Install
๐ฆ This includes:
- Language support for Javaโข
- Debugger for Java
- Maven & Gradle support
- Java Test Runner
- IntelliSense and formatting

๐ 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:
1 2 3 4 5 |
mkdir HelloJava21 cd HelloJava21 code . |
Inside VS Code, create a file Hello.java
:
1 2 3 4 5 6 7 8 |
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:
1 2 3 4 5 |
javac Hello.java java Hello |
If you’re using Maven:
1 2 3 4 5 |
mvn compile mvn exec:java |
๐ง Step 5: Explore Java 21 Features
Now you’re ready to use powerful features in Java 21!
โ Record Patterns
1 2 3 4 5 6 7 8 9 10 |
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)
1 2 3 4 5 |
Runnable task = () -> System.out.println("Running on " + Thread.currentThread()); Thread.startVirtualThread(task); |
โ Sequenced Collections
1 2 3 4 5 6 7 8 9 10 |
List<String> 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:
- Add a breakpoint (click to the left of the line number)
- Click the Run and Debug icon on the sidebar
- Select Java
- 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
:
1 2 3 4 5 6 7 |
{ "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.