Java in VS Code works well once three things line up: a JDK the editor can locate, the Extension Pack for Java, and a folder that is a recognisable project — Maven, Gradle, or a plain folder with a source root. When any one is off, symptoms range from no code completion to red underlines on every line to a run button that silently does nothing. The checks below go from the JDK outwards.
Step 1: the JDK#
java -version
javac -version
echo $JAVA_HOME # macOS/Linux
echo %JAVA_HOME% # Windows cmd
Both commands must work and report the same version, and it must be a JDK, not a JRE — javac missing means JRE. The Java extension needs Java 17 or later to run itself, even when your project targets an older version. If java is not recognised at all, fix that first; the editor cannot find what the terminal cannot.
If the JDK is installed somewhere VS Code does not look, tell it explicitly in settings:
{
"java.jdt.ls.java.home": "C:\\Program Files\\Eclipse Adoptium\\jdk-21.0.4.7-hotspot",
"java.configuration.runtimes": [
{ "name": "JavaSE-21", "path": "C:\\Program Files\\Eclipse Adoptium\\jdk-21.0.4.7-hotspot", "default": true }
]
}
The first setting is the JDK that runs the language server; the second is the list of JDKs available to your projects. Run Java: Configure Java Runtime from the command palette to see what the extension has detected.
Step 2: the extensions#
Install the Extension Pack for Java from Microsoft. It bundles the language server (Red Hat’s Language Support for Java), the debugger, the test runner, Maven and Gradle support, and a project manager. Installing only the language server, or only the debugger, produces a half-working setup.
After installing, open a .java file and watch the status bar. A small Java icon appears and shows the language server starting; hover over it for status. Then check the log: View, Output, and select Language Support for Java. Errors there name the actual problem.
Step 3: open the right folder#
This is the most common cause of “cannot find symbol” on every import. The language server needs a project to resolve classes against. It recognises:
- A folder containing
pom.xml(Maven). - A folder containing
build.gradleorbuild.gradle.kts(Gradle). - A plain folder with a
srcdirectory, treated as an unmanaged project.
Open that folder with File, Open Folder. Opening a single .java file, or the parent folder of several projects, gives the server nothing to work with. The status bar shows the loaded project name when it has one.
# A guaranteed-good test project
mkdir hello && cd hello && mkdir -p src
printf 'public class Hello { public static void main(String[] a) { System.out.println("hi"); } }' > src/Hello.java
code .
If completion works here and not in your project, the setup is fine and your project’s structure is what needs attention.
Errors that do not go away#
When the code compiles in the terminal but VS Code still shows red, the language server’s cached view of the project is stale:
- Run Java: Clean Java Language Server Workspace from the command palette and reload.
- If the project uses Maven, run Java: Reload Projects or click the refresh icon in the Java Projects view.
- For Lombok, the extension supports it automatically in current versions; if annotations are unresolved, update the extension pack.
Run and debug#
With the pack installed, a Run | Debug link appears above every main method, and F5 runs the current file. If nothing happens:
- Check the Debug Console for an error — often a missing dependency or the wrong JDK for the project’s
releasesetting. - Delete an old
.vscode/launch.jsonthat points at a class that no longer exists. - For a project that must be run through its build tool, use the terminal:
mvn spring-boot:run,./gradlew run.
Spring Boot#
Install the Spring Boot Extension Pack on top of the Java pack. It adds the Spring Boot Dashboard for starting and stopping applications, and understanding of Spring’s annotations and properties files. A Spring project that will not start from the dashboard but works with mvn spring-boot:run usually has a JDK mismatch: the project’s java.version in the pom against the JDK VS Code selected.
Slow or hanging#
The Java language server is a heavy process. First open of a large project can take a couple of minutes while it indexes; the status bar shows progress. If it hangs every time, give it more memory:
{ "java.jdt.ls.vmargs": "-Xmx2G -XX:+UseParallelGC" }
Questions people ask#
VS Code or IntelliJ for Java?
IntelliJ’s community edition is the stronger Java IDE, particularly for large projects and refactoring. VS Code is lighter and fine for learning, small projects and mixed-language work. Many people use both.
Which JDK should I install?
A current LTS: 21 or 25. Adoptium’s Temurin, Microsoft’s build and Oracle’s are all fine. The extension itself needs 17 or newer.
Can I use VS Code for a project targeting Java 8?
Yes. Install a 17+ JDK for the extension and a Java 8 JDK for the project, and list both in java.configuration.runtimes.
Why does the terminal use a different Java than the editor?
The terminal uses the PATH; the editor uses its settings. Make them agree by setting java.jdt.ls.java.home to the same JDK that which java reports.
Where to go next#
- “java is not recognized” — when the JDK is not on the PATH at all.
- “cannot find symbol” — when it is a real compile error, not an editor problem.
- Building a complete Java project — a proper Maven layout.