You build a Java project in Visual Studio Code by installing a Java Development Kit (JDK), adding Microsoft's Extension Pack for Java, then letting the extension's project tooling create or import the project, compile sources onto the classpath, and run it. If you already have a JDK on your PATH and the extension installed, the work is mostly folder layout and two commands: one to create or import, one to run. VS Code itself is an editor; the Java build comes from your installed JDK and the project configuration the extension reads, so success depends on both being set up correctly.

This guide uses JDK 21 as the illustrative version, the long-term-support release most tutorials target in late 2026, and a folder-based project rather than a build tool. Where Maven or Gradle changes the steps, the difference is called out. The extension identifiers and command names below are stable in the public documentation for current releases; exact UI wording can shift between VS Code versions, so verify the extension is enabled if a command does not appear.

What the build actually involves

A Java build has four moving parts: source files, a compiler from the JDK, class files, and a classpath that tells Java where compiled classes and libraries live. VS Code's Java extensions manage the last two. They scan src folders, infer packages from directory paths, hold a classpath model, and shell out to javac and java from your selected JDK.

That distinction explains the most common confusion. There is no separate "build button" that must be configured from scratch. Once the extension recognizes a folder as a Java source root, compiling is incremental and usually automatic. What you explicitly do is create the project structure, choose the JDK, and state where dependencies live if the project uses a tool like Maven. Source roots can be inferred or declared in the project's .classpath-style metadata, so keep src as the root rather than nesting sources under a directory named "code" or "java files" unless you adjust settings.

The result is a project that runs from VS Code's integrated terminal and from the run and debug launcher. Pick that runtime output as your success signal rather than the absence of red squiggles, since some compile errors appear only on save.

Prerequisites before touching VS Code

Install a JDK that matches your target language level. JDK 21 supports features through Java 21; code written with later features will not compile there. Oracle's and Eclipse Temurin's distributions both work, and VS Code's extension works with any JDK 17 or newer for the extension itself. Use a version at or above your project's language level, but not so new that older libraries fail to run on it. Set JAVA_HOME to the JDK installation folder and add its bin directory to PATH so javac -version and java -version report the same build in a newly opened terminal.

On Windows, the PATH is edited through System Properties; on macOS and Linux, export it in your shell profile. Reopen VS Code after changing environment variables, because terminals started before the change keep the old values. If javac reports a version different from java, an older JDK earlier on the PATH usually wins. That mismatch produces compile-then-runtime errors that look mysterious, such as UnsupportedClassVersionError, and the fix is PATH ordering, not a VS Code setting.

For a Maven or Gradle project, install the tool itself or let the extension's bundled handling manage it, and confirm mvn -v or gradle -v works in a terminal. You do not strictly need either for a single-folder project, but dependency management without one means downloading JARs and adding them to the classpath by hand, which is rarely worth it.

Step 1: Install the Extension Pack for Java

Open the Extensions view, search for "Extension Pack for Java" by Microsoft, and install it. The pack bundles the language support (syntax, completion, error checking), debugger, test runner, project manager, Maven support, and formatter. Installing only "Java" or only "Language Support for Java(TM) by Red Hat" leaves you without project creation and run tooling, which is the usual cause of a missing Java welcome page.

After installation, the Java Projects view appears in the Explorer sidebar and a Java welcome page appears once per workspace. If the sidebar is missing, reload the window. Behind the scenes the language server downloads a matching version of the Eclipse-based Java Language Server and an embedded JDK for its own operation, so the first launch may take a minute or two on a slower connection. A status spinner in the lower-right that ends with "Java: Ready" or an indexing message means it finished.

The extension requires VS Code 1.82 or later in current versions; if commands like Java: Create Java Project are absent, check the Extensions view for an update prompt or a disabled pitch.

Step 2: Create or open a Java project folder

For a new project, open the Command Palette (Ctrl+Shift+P, or Cmd+Shift+P on macOS) and run Java: Create Java Project. Choose No build tools for a plain folder layout, then select a project location, a project name, and a package name, or accept the defaults. The extension creates src with a package directory and a HelloWorld class inside it. If you use Maven or Gradle, choose those options instead; the extension generates pom.xml or build.gradle with a standard directory layout.

For an existing project, open its root folder with File > Open Folder, not the src subfolder. Opening too deep means the extension cannot see pom.xml or build.gradle, and imports fail or resolve libraries to nothing. After opening, a notification may offer to import the project or load classpath information; accept it.

A workable plain-project layout looks like this:

Packages must match directories: package com.example; belongs at src/com/example/. New files added by Java: New File or the class wizard query the package for you, which keeps this consistent.

Step 3: Confirm the JDK and compiler settings

Open a .java file once so the language server activates, then check the bottom status bar for a Java version indicator. Clicking it, or running Java: Configure Java Runtime, shows installed JDKs, the project's source level, and classpath libraries. Set the project JDK explicitly if the machine has several; selecting the wrong one produces language-level errors, such as "records are not supported" on JDK 8, and can prevent the extension from compiling at all.

The setting java.configuration.runtimes maps JDK versions to installation paths, for example an entry for JavaSE-21 pointing at the JDK's home directory, so the extension can offer that runtime per project. The java.jdt.ls.java.home setting, by contrast, controls the JDK the language server itself uses and should generally be at least JDK 17. Mixing these two up is a common source of startup errors; the first is per-project runtime, the second is editor infrastructure.

If the status bar shows "No JDK" or a welcome page warns about an incompatible JDK, fix JAVA_HOME and the runtime setting before touching project code. The compile step depends on a working javac, so this check saves time.

Step 4: Write a minimal working class

Confirm compilation with something small. In the package folder, create App.java:

Saving triggers syntax checking. Compile errors appear as red squiggles, and the Problems panel lists them with file and line. Missing braces and misspelled package names are the most frequent problems for new setups. A class without a matching file name, or a package declaration that does not match the folder, produces errors that VS Code reports clearly; fix those before building further.

If the class shows no completion and every import is unresolved, the extension has not recognized the source root. Reload the window, or check that the folder is open at project level and contains src as configured.

Step 5: Build from the command line

The reproducible build check is the terminal. Open a terminal at the project root with Terminal > New Terminal. For the plain layout, compile all sources into an output directory:

On Windows PowerShell, the wildcard form differs; use javac -d out (Get-ChildItem -Recurse src -Filter *.java | ForEach-Object FullName). Then run:

You should see build works. An empty or missing out directory, or a message that the class cannot be found, means compilation failed silently or ran from the wrong directory; check that src exists relative to the terminal's working directory. java -version here should match the JDK you compiled with.

This two-command pattern scales for small projects but becomes unwieldy as dependencies multiply. At that point, Maven or Gradle should own the build, and VS Code's run tooling invokes them rather than javac directly.

Step 6: Run and debug inside VS Code

To run without the terminal, open the class with the main method and use the play button next to it, or run Java: Run Java File from the Command Palette. A Run and Debug session starts, and output appears in the Debug Console or terminal. Clicking in the gutter sets a breakpoint; the debugger stops there and shows variables and call stack. This uses the launch configuration the extension generated in .vscode/launch.json, which you can inspect and adjust.

If a run produces "Main class not found," the launch configuration points at the wrong class or project. The className field must be fully qualified, package included. If the run opens a different project's class, ensure the correct project folder is the active workspace rather than a subfolder or a recently opened sibling project.

Building from VS Code's Terminal menu with Run Build Task uses the generated build task, which for Maven runs mvn compile and for plain projects compiles the detected source roots. It is a convenience over manually typing the javac line, not a different build engine.

Choosing between plain folders, Maven, and Gradle

These are genuinely different workflows with different trade-offs. Match them on criteria, not vibes.

A plain folder project is best when you are learning, prototyping, or writing single-file exercises. Setup is one wizard and the build is javac. It has no dependency management; third-party libraries must be placed in a folder the classpath knows about, and versions are manual. Maven centralizes dependency declarations in pom.xml, has the largest ecosystem of plugins, and enforces a conventional layout, at the cost of XML verbosity and more abstraction than beginners need. Gradle uses Groovy or Kotlin DSL scripts, builds faster in large multilayered projects, and is more flexible, but its scripts are code and therefore more error-prone for newcomers.

VS Code's extension treats all three similarly for editing, but the build command differs: mvn compile or gradle build replace the javac line, and dependency classpaths come from the tool. For Maven, the generated .classpath metadata under the target folder is managed for you. If a Maven import fails, run Maven: Reload Projects from the Command Palette; it re-fetches dependencies, which is the reversible first check for missing libraries.

Common build failures and what to change first

Wrong JDK or mixed versions. Symptom: class file version errors or syntax features unavailable despite correct source. Check: javac -version and java -version in a fresh terminal, and the runtime shown in the status bar. Fix the PATH or the runtime setting. Use UnsupportedClassVersionError as the stop condition to re-verify the JDK before altering source code.

Sources under an unexpected folder. Symptom: everything compiles only after opening files one by one, or not at all. Check: the src folder is present at the workspace root. The extension looks for the standard layout; custom source folders need settings like java.project.sourcePaths.

Unresolved imports for libraries. Symptom: imports red, but code was fetching dependencies earlier. Check: internet access and Maven: Reload Projects or Gradle: Refresh Gradle Project; a corrupted local repository or proxy config blocks downloads. Clean the specific dependency cache rather than deleting indiscriminately, since that resolves the wrong-version classpath issue but can re-download slowly.

Launcher cannot find the class. Symptom: "Error: Could not find or load main class." Check: package declaration matches directory; -cp points at the out root and not inside the package folder; main signature is public static void main(String[] args).

Language server stuck indexing. Symptom: no completion or errors reported on unrelated files. Check: disable other Java-adjacent extensions that duplicate the language server, then use Java: Clean Java Language Server Workspace. A status spinner that ends in an indexing error message rather than completion indicates the workspace cache needs a reset.

Where a professional becomes the right next step

Stop doing manual environment surgery when the failure crosses into build tooling you did not create, such as a corporate Maven repository with custom authentication, a Gradle build requiring a plugin version matrix, or a JDK defect that recurs after a clean install. At that point, a team build engineer or the library maintainer's support channel can reproduce the failure faster than trial and error, and reversible checks have already been exhausted. Similarly, if javac itself crashes with a JVM error, the JDK installation is the issue and reinstalling or verifying checksums, rather than editing VS Code settings, is warranted.

Practical closing notes

Two habits make this setup durable. First, commit the workspace configuration that matters, such as a tested launch configuration, so teammates get a working run button instead of instructions. Second, treat the terminal build command as the source of truth; when it passes and VS Code's run does not, the difference is launch configuration, and when it fails, the error message from javac or mvn is more precise than the squiggle. For a dependency-heavy project, let Maven or Gradle own the build and let the extension consume it. For a quick exercise, the plain folder layout and a two-line compile-and-run pair are the least friction you can get while still building a real classpath.