VSCode

How to Run Java in VSCode: A Step-by-Step Guide

How to Run Java in VSCode: A Step-by-Step Guide

Setting up Java in VS Code takes less than 10 minutes, but only if you know exactly what to install and why.

Visual Studio Code has become the most-used code editor globally, with 75.9% of developers choosing it in 2025 (Stack Overflow). Java developers are following. Running the Java compilation and execution workflow inside VS Code is now a legitimate alternative to heavier IDEs.

This guide covers how to run Java in VS Code from scratch, including JDK setup, the Extension Pack for Java, project configuration, terminal-based execution, and debugging.

By the end, you’ll have a working Java development environment and know how to fix the most common errors that trip people up along the way.

What You Need Before Running Java in VS Code

maxresdefault How to Run Java in VSCode: A Step-by-Step Guide

Visual Studio Code has over 2.5 million Java developers actively using it, according to Microsoft. That’s a lot of people who needed to figure out the same setup you’re about to do.

Before writing a single line of Java, you need two things installed on your machine: VS Code itself and a JDK. Not a JRE. A JDK.

JDK vs JRE: Why It Matters

JRE (Java Runtime Environment) runs Java programs. That’s it. You can’t compile with it.

JDK (Java Development Kit) includes the compiler (javac), the runtime, and all the tools you need to write and build Java applications. This is what you install.

Common mistake: downloading the JRE thinking it’s enough, then wondering why javac isn’t found. Get the JDK.

Which JDK Version to Use

Stick to LTS versions. Non-LTS Java versions are used by fewer than 2% of production applications, per New Relic’s 2024 State of the Java Ecosystem report.

JDK VersionTypeStatusBest For
JDK 11LTSStill used, agingLegacy projects
JDK 17LTS35% of apps in production (New Relic 2024)Enterprise standard
JDK 21LTS45% adoption rate (Second Talent 2025)New projects

For new projects, JDK 21 is the safe bet. It’s the current LTS release, and adoption is growing fast.

Where to get it: Eclipse Adoptium (community-managed, free, updated frequently), Oracle JDK (official, but licensing restrictions apply for commercial use), or Amazon Corretto (free, production-ready). Eclipse Adoptium saw a 50% year-over-year growth in 2024, making it a solid default choice.

Why the Extension Pack for Java Is Non-Negotiable

VS Code on its own has zero Java build or run capability. The Extension Pack for Java, published by Microsoft, is what makes Java development actually work in VS Code.

Without it, you’re just editing a text file.

Install it before anything else. The entire setup experience in the sections below assumes it’s already there.

Every shortcut and workflow trick from this guide - including Ctrl+P for quick open, Ctrl+Shift+P for the command palette, and the full grid of keyboard shortcuts - is on one page in the VS Code Workflow Cheat Sheet.

How to Install the Java Extension Pack in VS Code

maxresdefault How to Run Java in VSCode: A Step-by-Step Guide

The Extension Pack for Java is a bundle of six extensions maintained by Microsoft and Red Hat. Install it once and you have everything for core Java development.

What’s Inside the Extension Pack

Language Support for Java (Red Hat): code completion, IntelliSense, error highlighting, go-to-definition, hover documentation. The core engine.

Debugger for Java (Microsoft): breakpoints, step-through debugging, the Debug Console. Without this, you can only run, not debug.

Java Test Runner: runs JUnit and TestNG tests directly from VS Code with inline results.

Maven for Java: Maven project support, run goals from a sidebar panel.

Gradle for Java: Gradle build support, added to the pack in 2024 after community feedback.

Project Manager for Java: handles project creation, switching, and workspace management.

Step-by-Step Install

Open VS Code. Press Ctrl+Shift+X (Windows/Linux) or Cmd+Shift+X (Mac) to open the Extensions sidebar.

Search for “Extension Pack for Java”. Look for the one published by Microsoft, not third-party alternatives. Click Install.

VS Code installs all six extensions at once. This takes 30-60 seconds depending on your connection.

After install, you’ll see a “Java” section appear in your sidebar and a Java status bar item at the bottom of the editor. That’s how you know it worked.

Verify the Install

Open the Command Palette with Ctrl+Shift+P and type “Java: Tips for Beginners”. If that command shows up, the extension pack loaded correctly.

If the command doesn’t appear, try reloading the window first. Ctrl+Shift+P → “Developer: Reload Window”. That fixes most post-install issues.

How to Set Up the JDK Path in VS Code

maxresdefault How to Run Java in VSCode: A Step-by-Step Guide

Installing a JDK on your machine isn’t enough. VS Code needs to know where it is.

This is the step most people skip and then spend 20 minutes wondering why the Java Language Server won’t start.

Using the VS Code Java Runtime UI

Open the Command Palette (Ctrl+Shift+P) and run “Java: Configure Java Runtime”. A settings panel opens showing detected JDKs.

If your JDK was installed in a standard location (like C:\Program Files\Java on Windows or /usr/lib/jvm on Linux), VS Code often detects it automatically.

If it doesn’t appear, click “Add JDK” and point it to your installation folder.

Setting the Path in settings.json

For more control, open settings.json in VS Code and add this manually:

"java.jdt.ls.java.home": "/path/to/your/jdk"

Replace the path with your actual JDK location. On Windows, use forward slashes or escape backslashes. Example: C:/Program Files/Java/jdk-21.

Key differencejava.jdt.ls.java.home controls the JDK used by the Java Language Server itself. java.configuration.runtimes controls which JDK is used to build and run your projects. They can be different versions.

Handling Multiple JDK Versions

Running multiple JDKs on the same machine is common. Per the 2025 Java Developer Productivity Report, 42% of Java developers use more than one IDE or toolchain setup in their workflow.

To manage multiple versions, add an array to settings.json:

"java.configuration.runtimes": [
{ "name": "JavaSE-17", "path": "/path/to/jdk-17" },
{ "name": "JavaSE-21", "path": "/path/to/jdk-21", "default": true }
]

The "default": true flag tells VS Code which version to use for new projects that don’t specify one.

Common Path Errors

Two errors come up constantly:

  • “No Java runtime could be found”: your path points to the JRE folder inside the JDK, not the JDK root. Go up one level.
  • “Build failed” on startup: JAVA_HOME environment variable is set to a different JDK than what VS Code expects. Align them or remove JAVA_HOME from your system variables and let VS Code handle it.

How to Create and Run a Basic Java File

maxresdefault How to Run Java in VSCode: A Step-by-Step Guide

No project setup. No build tool. Just a file. This is the fastest way to confirm your setup actually works before touching Maven or Gradle.

Create Your First Java File

Create a new folder on your machine, open it in VS Code with File → Open Folder.

Inside that folder, create a new file and name it Hello.java. The filename must match the class name, including capitalization. This is not optional. Java enforces this.

Paste in the following:

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

Save the file. Within a few seconds, the Java Language Server loads and you’ll see syntax highlighting kick in.

Running with the Run Button vs the Terminal

MethodHowBest For
Run buttonClick “Run” above the main methodQuick execution, no setup
F5Keyboard shortcut, triggers debuggerDebugging with breakpoints
Terminal (javac + java)Compile then run manuallyUnderstanding the build process

The “Run” link appears right above the main method once the Extension Pack loads. It’s a CodeLens feature from Red Hat’s Language Support extension. Click it and output appears in the Terminal panel below.

Running from the Terminal

Open the VS Code terminal with Ctrl+` . Compile your file first:

javac Hello.java

This creates Hello.class in the same folder. Then run it:

java Hello

Note: no .class extension when running. Just the class name.

You should see Hello, World! printed in the terminal. If you do, your JDK path is correctly configured and everything is working.

What Errors Look Like

Red squiggles in the editor: syntax errors caught by the Language Server before you even run anything.

“error: class Hello is public, should be declared in a file named Hello.java”: filename doesn’t match class name. Rename the file.

“javac is not recognized”: your JDK’s bin folder isn’t in the system PATH. Either fix PATH or use the Run button instead of the terminal.

How to Run Java Using a Project Structure

Single files work for testing. Anything real needs a proper project. VS Code handles this through the Extension Pack, and the setup is faster than most people expect.

Creating a Java Project via Command Palette

Open the Command Palette (Ctrl+Shift+P) and run “Java: Create Java Project”.

You’ll be prompted to choose a project type:

  • No build tools (simple folder structure)
  • Maven
  • Gradle
  • Spring Boot (if Spring Extension Pack is installed)

For learning, “No build tools” is fine. For anything you’ll actually ship or collaborate on, use Maven or Gradle.

VS Code Java Project Folder Structure

A standard Java project created by VS Code looks like this:

my-project/
├── src/
│   └── App.java
├── bin/
└── .classpath

Source files live in src/. Compiled .class files go into bin/. The Extension Pack manages this automatically. You don’t configure the output folder manually.

Key difference from single files: when running a project, VS Code compiles the entire src/ directory, not just the file you have open. This matters once you have multiple classes referencing each other.

Maven Projects

Maven is the most widely used Java build tool in enterprise environments. Software development teams working on larger Java codebases almost always use it.

To create a Maven project: Command Palette → “Java: Create Java Project” → Maven → choose an archetype. The simplest is maven-archetype-quickstart.

VS Code creates a standard Maven structure with a pom.xml file. The Maven sidebar panel (visible on the left) lets you run goals like compiletest, and package without touching the terminal.

To run your main class via terminal:

mvn exec:java -Dexec.mainClass="com.example.App"

Gradle Projects

Gradle is faster than Maven for large builds and is the default for Android development. If you’ve worked on Android development, you already know Gradle well.

Create a Gradle project the same way: Command Palette → “Java: Create Java Project” → Gradle.

The Gradle for Java extension adds a dedicated panel in the sidebar. Expand your project there and click any task to run it. buildruntest are all available without memorizing commands.

To run from terminal:

./gradlew run

On Windows, use gradlew.bat run instead.


How to Run Java in the VS Code Terminal

maxresdefault How to Run Java in VSCode: A Step-by-Step Guide

Sometimes you want to skip the GUI entirely. Running Java manually through the terminal is slower for daily work but useful when you need to understand exactly what’s happening, pass custom flags to the JVM, or debug classpath issues.

Compiling and Running a Single File

Open the integrated terminal in VS Code and navigate to your source file’s directory.

Compile:

javac FileName.java

Run:

java FileName

Two steps, every time. javac compiles. java runs. Never skip the compile step after editing.

Running Files Inside Packages

This trips people up. If your Java file starts with a package declaration like package com.example;, you can’t just run java MyClass from that folder.

You need to compile from the project root and run using the fully qualified class name:

javac src/com/example/MyClass.java -d bin/
java -cp bin com.example.MyClass

The -cp flag sets the classpath. It tells the JVM where to find compiled .class files. Getting this wrong is the #1 source of “ClassNotFoundException” errors when running from the terminal.

When Terminal Execution Is More Useful Than the GUI

Three scenarios where manual terminal execution beats clicking Run:

  • Custom JVM flags: passing -Xmx512m to limit heap size, or -ea to enable assertions
  • Classpath debugging: you can see exactly what’s on the path and catch missing dependencies before they hit runtime
  • CI/CD parity: your build pipeline runs javac and java directly; testing the same commands locally catches environment mismatches early

For anything more complex than a class or two, switching to Maven or Gradle (covered in the previous section) saves significant time. Terminal compilation doesn’t scale well once you have multiple source files and external dependencies.

How to Debug Java in VS Code

maxresdefault How to Run Java in VSCode: A Step-by-Step Guide

Debugging is where VS Code actually punches above its weight for Java. 84% of IntelliJ IDEA users name VS Code as their secondary IDE, with debugging cited as a primary reason, according to the 2024 Java Developer Productivity Report by JRebel.

The Debugger for Java extension (part of the Extension Pack) supports breakpoints, expression evaluation, hot code replacement, conditional breakpoints, and thread inspection. That covers almost everything you’d need day-to-day.

Setting Breakpoints

Click in the gutter (the left margin next to line numbers) to set a breakpoint. A red dot appears. That’s it.

Four breakpoint types are available:

  • Line breakpoints (the default)
  • Conditional breakpoints: pauses only when an expression is true
  • Data breakpoints: triggers when a variable’s value changes
  • Logpoints: prints to the Debug Console without pausing execution

For most debugging sessions, line breakpoints and conditional breakpoints cover 90% of use cases.

Launch vs Attach Debug Configurations

Launch mode: VS Code starts the JVM itself. Works for most standalone Java apps and Maven/Gradle projects. Press F5 and it runs.

Attach mode: VS Code connects to an already-running JVM. The JVM must be started with JDWP arguments:

-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5005

Then in launch.json, use "request": "attach" with the host and port. Attach mode is useful for debugging running servers, Docker containers, or remote environments.

ModeHow It WorksBest For
LaunchVS Code starts the JVMLocal apps, quick runs
Attach (local)Connects to running local JVMApp servers, Tomcat
Attach (remote)Connects over networkDocker, staging environments

Reading the Debug Console

The Debug Console shows program output, evaluation results, and JVM messages. It accepts expressions for evaluation, which is useful for inspecting objects mid-execution.

Important: the Debug Console doesn’t accept standard input by default. If your program reads from stdin, change the console setting in launch.json to "integratedTerminal".

The Variables panel on the left shows all variables in scope. Hover over any variable in the editor to see its current value inline. No need to add print statements.

Common Debug Setup Issues

VS Code auto-generates launch.json for most projects. You only need to create it manually for edge cases like multi-module projects or attach configurations.

Two issues come up constantly:

  • “Build failed, do you want to continue?”: there’s a compilation error in your code. Check the Problems panel (Ctrl+Shift+M), fix the errors, then run again. Don’t click Continue, it usually leads nowhere useful.
  • Conditional breakpoints not triggering: if your workspace has multiple Java projects, add "projectName" to launch.json. Without it, expression evaluation is unreliable across projects.

Common Errors When Running Java in VS Code

maxresdefault How to Run Java in VSCode: A Step-by-Step Guide

Most Java setup errors in VS Code fall into three categories: JDK detection failures, classpath problems, and Language Server issues.

Knowing which category an error belongs to cuts troubleshooting time significantly. The JRebel 2024 Java Developer Productivity Report found that 29% of Java developers name too many unresolved environment issues as their top productivity barrier.

“No Java Runtime Could Be Found”

This one usually means one of three things:

  • JDK isn’t installed yet (only JRE was installed)
  • The JDK path in settings points to the wrong folder
  • JAVA_HOME environment variable points to a JRE, not a JDK

Fix: run java -version in your terminal. If that works, run javac -version. If javac fails, you have a JRE, not a JDK. Install a JDK and point java.jdt.ls.java.home to it in settings.json.

“Build Failed, Do You Want to Continue?”

Two distinct causes here, and they need different fixes.

Compilation errors: your Java code has syntax issues. Open the Problems panel and fix them. The error list tells you exactly what’s wrong and on which line.

Project configuration errors: the Language Server can’t parse your project structure. Run “Java: Clean Java Language Server Workspace” from the Command Palette, then reload VS Code. This clears the stale workspace cache and forces a full rebuild.

Red Squiggles That Won’t Go Away

Persistent red squiggles after a fresh install are almost always a Language Server loading problem, not an actual code error.

Check the status bar at the bottom of VS Code. If you see a spinning icon next to the Java status indicator, the Language Server is still indexing. Wait for it to finish before judging any errors.

If squiggles persist after the Language Server finishes:

  • Run “Java: Update Project Configuration” from the Command Palette
  • If using Maven, right-click pom.xml and select “Update Project Configuration”
  • Last resort: “Java: Clean Java Language Server Workspace” and reload

ClassNotFoundException and Classpath Issues

This error at runtime means the JVM can’t find a class it needs. Classpath configuration is the codebase-level issue that trips up the most developers moving from full IDEs to VS Code.

ErrorLikely CauseFix
ClassNotFoundExceptionMissing dependency in classpathAdd JAR to Referenced Libraries or update pom.xml/build.gradle
NoClassDefFoundErrorClass compiled but not found at runtimeCheck -cp flag or project output folder
File not on classpathJava file outside source rootRight-click folder → “Add Folder to Java Source Path”

For Maven and Gradle projects, VS Code handles the classpath automatically from pom.xml or build.gradle. Manual classpath management is only needed for projects without build tools.

VS Code Java vs IntelliJ: When to Switch

VS Code works well for learning, lightweight Java work, multi-language projects, and Spring Boot with the Spring Extension Pack. The 2025 Java Developer Productivity Report shows 31% of Java developers now use VS Code, up from 19% the year before.

That said, for large enterprise applications with complex module graphs, heavy Spring Boot configuration, or Jakarta EE, IntelliJ IDEA offers deeper tooling that VS Code doesn’t fully replicate yet. The gap is narrowing, but it’s real.

For most new Java projects, VS Code is a perfectly capable environment. Especially if you’re already comfortable with the editor from Python or JavaScript work in VS Code. The learning curve for Java setup is genuinely the steepest part. Once you’re past it, the editor gets out of your way.

FAQ on How To Run Java In VS Code

Do I need to install a JDK to run Java in VS Code?

Yes. VS Code has no built-in Java runtime. You need a JDK (not just a JRE) installed on your machine. The Extension Pack for Java supports JDK 1.8 and above. JDK 17 or JDK 21 are the recommended choices for new projects.

What extension do I need to run Java in VS Code?

Install the Extension Pack for Java, published by Microsoft. It bundles Language Support for Java (Red Hat), the Debugger for Java, Java Test Runner, Maven for Java, Gradle for Java, and Project Manager for Java.

How do I run a single Java file without a project?

Create a .java file, open it in VS Code, and click the Run link that appears above the main method. No project setup needed. The Java Language Server handles compilation automatically in the background.

Why does VS Code say “No Java runtime could be found”?

This usually means VS Code can’t locate your JDK. Open settings.json and set "java.jdt.ls.java.home" to your JDK installation path. Make sure the path points to the JDK root folder, not the bin subfolder.

What does “Build failed, do you want to continue?” mean?

It means your workspace has compilation errors or a project configuration issue. Open the Problems panel (Ctrl+Shift+M) and fix the listed errors. Running “Java: Clean Java Language Server Workspace” from the Command Palette also resolves most persistent build failures.

Can I run Java in the VS Code terminal manually?

Yes. Compile with javac FileName.java, then run with java FileName. For files inside packages, compile from the project root using the -d flag and run with the fully qualified class name using the -cp flag.

How do I run a Maven or Gradle Java project in VS Code?

For Maven, use the Maven sidebar panel or run mvn exec:java -Dexec.mainClass="com.example.App" in the terminal. For Gradle, use the Gradle panel or run ./gradlew run. Both require their respective extensions from the Extension Pack.

How do I debug Java in VS Code?

Set a breakpoint by clicking the gutter next to a line number. Press F5 to start a debug session. VS Code auto-generates a launch.json configuration for most projects. Use the Variables panel and Debug Console to inspect runtime state.

Is VS Code good enough for Java development, or should I use IntelliJ?

VS Code works well for learning, lightweight Java projects, and multi-language workflows. For large enterprise codebases with complex Spring Boot or Jakarta EE configurations, IntelliJ IDEA provides deeper tooling. The 2025 Java Developer Productivity Report shows 31% of Java developers now use VS Code.

Which JDK version should I use with VS Code?

Use an LTS version. JDK 21 is the current recommended choice for new projects, with 45% adoption as of 2025 (Second Talent). JDK 17 remains the most widely used in production at 35% of applications, per New Relic’s 2024 State of the Java Ecosystem report.

 

Conclusion

This conclusion is for an article presenting how to run Java in VS Code, a setup that’s more capable than most developers expect from a lightweight editor.

The foundation is simple: install a JDK (preferably JDK 21), add the Extension Pack for Java, configure your runtime path, and you’re running Java programs within minutes.

From there, whether you’re compiling single files, managing Maven or Gradle projects, or stepping through code with the Java debugger, VS Code handles the full java development workflow without the overhead of a dedicated IDE.

Most java compiler errors and classpath issues have straightforward fixes once you know where to look.

The java runtime configuration and Language Server are the two things worth understanding well. Everything else follows naturally.

50218a090dd169a5399b03ee399b27df17d94bb940d98ae3f8daff6c978743c5?s=250&d=mm&r=g How to Run Java in VSCode: A Step-by-Step Guide

Stay sharp. Ship better code.

Every week: one curated article, one tool worth knowing, one tip you can use tomorrow. No noise, no padding.