
Visual Studio Code isn’t a full IDE like IntelliJ or Eclipse. It’s a lightweight editor that becomes a capable Java development environment once you install the right extensions. For beginners and developers who prefer speed over features, it’s a solid choice.
This guide walks through the complete setup process on Windows, macOS, and Linux.
Prerequisites
Before configuring VS Code, you need Java installed on your system. If you haven’t done this yet, follow our Java installation guide first.
Verify your installation by opening a terminal and running:
java -version
You should see output showing Java 17 or later. If you get “command not found,” Java isn’t installed or isn’t in your system PATH.
Installing VS Code
Download VS Code from code.visualstudio.com. The site detects your operating system and offers the correct installer.
Windows: Run the .exe installer. Accept the defaults, but check “Add to PATH” when prompted. This lets you open VS Code from the command line.
macOS: Open the .dmg file and drag VS Code to your Applications folder. To enable command-line access, open VS Code, press Cmd+Shift+P, type “shell command,” and select “Install ‘code’ command in PATH.”
Linux: Download the .deb (Debian/Ubuntu) or .rpm (Fedora/RHEL) package. Install with your package manager, or use Snap: sudo snap install code --classic.
Installing the Java Extension Pack
VS Code needs extensions to understand Java. Microsoft provides an official extension pack that bundles everything you need.
Open VS Code and click the Extensions icon in the left sidebar (or press Ctrl+Shift+X). Search for “Extension Pack for Java” and install the one published by Microsoft.
This pack includes six extensions:
- Language Support for Java: Syntax highlighting, code completion, error checking
- Debugger for Java: Breakpoints, variable inspection, step-through debugging
- Test Runner for Java: Run JUnit and TestNG tests
- Maven for Java: Maven project support
- Project Manager for Java: Create and manage Java projects
- IntelliCode: AI-assisted code completion
After installation, VS Code may prompt you to reload. Do so.
Configuring the JDK
VS Code needs to know where your JDK is installed. Usually it detects this automatically. If not, you’ll need to configure it manually.
Open Settings (Ctrl+Comma or Cmd+Comma on macOS). Search for “java.jdt.ls.java.home” and set it to your JDK installation path:
Windows: C:\Program Files\Java\jdk-21
macOS: /Library/Java/JavaVirtualMachines/jdk-21.jdk/Contents/Home
Linux: /usr/lib/jvm/java-21-openjdk
Your exact path depends on the JDK version and how you installed it. To find the path, run which java on macOS/Linux or where java on Windows, then navigate up to the JDK root directory.
Alternatively, add this to your settings.json file (Ctrl+Shift+P, then “Preferences: Open Settings (JSON)”):
{
"java.jdt.ls.java.home": "/path/to/your/jdk",
"java.configuration.runtimes": [
{
"name": "JavaSE-21",
"path": "/path/to/your/jdk",
"default": true
}
]
}
Creating Your First Project
VS Code offers several ways to create Java projects. The simplest approach for beginners:
Press Ctrl+Shift+P (Cmd+Shift+P on macOS) to open the Command Palette. Type “Java: Create Java Project” and select it. Choose “No build tools” for a basic project without Maven or Gradle.
Select a folder location and enter a project name. VS Code creates a project structure:
my-project/
src/
App.java
lib/
README.md
Open App.java. You’ll see a basic Hello World program:
public class App {
public static void main(String[] args) throws Exception {
System.out.println("Hello, World!");
}
}
Running Java Code
VS Code provides multiple ways to run Java programs.
Method 1: Click “Run” above main()
VS Code displays “Run | Debug” links above any main method. Click “Run” to execute the program. Output appears in the integrated terminal.
Method 2: Right-click and Run
Right-click anywhere in the editor and select “Run Java.” This works for any file containing a main method.
Method 3: Use the Run menu
Go to Run > Run Without Debugging (Ctrl+F5). This runs the current file.
Method 4: Terminal
Open the integrated terminal (Ctrl+`) and compile/run manually:
cd src
javac App.java
java App
For quick tests, the click-to-run approach is fastest. For understanding what’s happening, the terminal approach teaches you the underlying process.
Using the Debugger
VS Code’s Java debugger works like those in full IDEs. You can set breakpoints, step through code, and inspect variables.
Setting Breakpoints
Click in the gutter to the left of a line number. A red dot appears, marking a breakpoint. Execution will pause when it reaches this line.
public class DebugExample {
public static void main(String[] args) {
int sum = 0;
for (int i = 1; i <= 10; i++) {
sum += i; // Set breakpoint here
}
System.out.println("Sum: " + sum);
}
}
Starting the Debugger
Click “Debug” above the main method (instead of “Run”). Or press F5. The program runs until it hits your breakpoint.
Debug Controls
When paused at a breakpoint, a toolbar appears with these buttons:
Continue (F5): Resume execution until the next breakpoint.
Step Over (F10): Execute the current line and move to the next.
Step Into (F11): Enter method calls to see what happens inside.
Step Out (Shift+F11): Finish the current method and return to the caller.
Restart (Ctrl+Shift+F5): Stop and restart debugging from the beginning.
Stop (Shift+F5): End the debugging session.
Inspecting Variables
While paused, the left sidebar shows the Variables panel. It displays all variables in the current scope with their values. You can expand objects to see their fields.
Hover over any variable in the code to see its current value. This works for primitives, strings, and complex objects.
Useful Settings
These settings improve the Java development experience in VS Code. Add them to your settings.json or search for them in the Settings UI.
{
"java.compile.nullAnalysis.mode": "automatic",
"java.format.onType.enabled": true,
"java.saveActions.organizeImports": true,
"editor.formatOnSave": true,
"editor.minimap.enabled": false,
"editor.fontSize": 14,
"editor.tabSize": 4,
"files.autoSave": "afterDelay"
}
java.compile.nullAnalysis.mode: Warns about potential null pointer issues.
java.format.onType.enabled: Formats code as you type, adding closing braces and proper indentation.
java.saveActions.organizeImports: Automatically adds missing imports and removes unused ones when you save.
editor.formatOnSave: Cleans up formatting every time you save a file.
Working with Multiple Files
Real projects have multiple classes. VS Code handles this well once you understand the structure.
Create new Java files by right-clicking the src folder and selecting “New File.” Name it with the .java extension. The filename must match the public class name inside.
// File: src/Calculator.java
public class Calculator {
public int add(int a, int b) {
return a + b;
}
public int multiply(int a, int b) {
return a * b;
}
}
// File: src/App.java
public class App {
public static void main(String[] args) {
Calculator calc = new Calculator();
System.out.println("5 + 3 = " + calc.add(5, 3));
System.out.println("5 * 3 = " + calc.multiply(5, 3));
}
}
VS Code automatically compiles dependent files when you run the main class. You don’t need to compile each file separately.
Adding External Libraries
For projects without Maven or Gradle, you add JAR files manually.
Download the JAR file you need and place it in your project’s lib folder. Then configure VS Code to include it in the classpath.
Create or edit the file .vscode/settings.json in your project root:
{
"java.project.referencedLibraries": [
"lib/**/*.jar"
]
}
This tells VS Code to include all JAR files in the lib folder. Restart VS Code or reload the window for changes to take effect.
For larger projects with many dependencies, consider using Maven or Gradle instead. They handle dependency management automatically.
Keyboard Shortcuts
Learning these shortcuts speeds up your workflow considerably:
| Action | Windows/Linux | macOS |
|---|---|---|
| Run without debugging | Ctrl+F5 | Ctrl+F5 |
| Start debugging | F5 | F5 |
| Toggle breakpoint | F9 | F9 |
| Quick fix / suggestions | Ctrl+. | Cmd+. |
| Go to definition | F12 | F12 |
| Find all references | Shift+F12 | Shift+F12 |
| Rename symbol | F2 | F2 |
| Format document | Shift+Alt+F | Shift+Option+F |
| Organize imports | Shift+Alt+O | Shift+Option+O |
| Open Command Palette | Ctrl+Shift+P | Cmd+Shift+P |
Troubleshooting Common Issues
“Java runtime could not be located”
VS Code can’t find your JDK. Set the java.jdt.ls.java.home setting to your JDK path, or ensure JAVA_HOME is set in your system environment variables.
Red squiggles everywhere, but code compiles fine
The Java language server may need a restart. Press Ctrl+Shift+P, type “Java: Clean Java Language Server Workspace,” and select it. Confirm the restart.
IntelliSense / autocomplete not working
Wait for the Java language server to initialize. Look for “Java” in the status bar at the bottom. If it shows a loading spinner, it’s still starting up. Large projects take longer.
Cannot find symbol errors for other classes in the same project
Make sure all .java files are in the src folder. VS Code needs them in the correct location to recognize them as part of the same project.
VS Code vs Full IDEs
VS Code works well for learning Java, small projects, and developers who value a fast, minimal editor. It starts quickly and uses less memory than IntelliJ or Eclipse.
For large enterprise projects, complex refactoring, or advanced framework support (Spring Boot, Jakarta EE), a full IDE provides more power. Many developers use VS Code for quick edits and IntelliJ for serious development.
Start with VS Code. If you find yourself wanting features it doesn’t have, try IntelliJ IDEA Community Edition. It’s free and more full-featured.
Related: How to Install Java | Your First Java Program: Hello World | Best Java IDEs Compared | How to Debug Java Code
Sources
- Microsoft. “Java in Visual Studio Code.” code.visualstudio.com/docs/languages/java
- Microsoft. “Getting Started with Java in VS Code.” code.visualstudio.com/docs/java/java-tutorial
- Red Hat. “Language Support for Java.” marketplace.visualstudio.com


