Setting Up JDK, PATH, and VS Code
Before you can write and run Java programs, you need a working development environment. This guide walks you through installing the JDK, configuring the PATH environment variable, and setting up Visual Studio Code as your Java editor.
Step 1: Install the JDK
The JDK (Java Development Kit) contains everything you need to compile and run Java code, including the compiler (javac) and the runtime (java).
Choose a Distribution
Java has several distributions built from the same OpenJDK source. Popular choices include:
- Eclipse Temurin (Adoptium) — free, widely used, and well-supported.
- Oracle JDK — official Oracle build.
- Amazon Corretto, Microsoft Build of OpenJDK, Azul Zulu — production-ready alternatives.
Pick a Long-Term Support (LTS) version such as Java 17 or Java 21 for stability.
Install It
Windows
- Download the
.msiinstaller from your chosen distribution. - Run the installer and follow the prompts.
- Note the install location, typically
C:\Program Files\Eclipse Adoptium\jdk-21....
macOS
- Download the
.pkginstaller, or use Homebrew:brew install temurin - The JDK is installed under
/Library/Java/JavaVirtualMachines/.
Linux
- Use your package manager, for example on Ubuntu/Debian:
sudo apt update sudo apt install temurin-21-jdk - Or download the
.tar.gz, extract it, and place it in/opt/.
Step 2: Configure the PATH
The PATH environment variable tells your operating system where to find the java and javac commands so you can run them from any directory.
Set JAVA_HOME
JAVA_HOME points to your JDK installation folder. Many tools (Maven, Gradle, IDEs) rely on it.
Windows
- Open Start → search Environment Variables → Edit the system environment variables.
- Click Environment Variables.
- Under System variables, click New:
- Variable name:
JAVA_HOME - Variable value:
C:\Program Files\Eclipse Adoptium\jdk-21...
- Variable name:
- Find the
Pathvariable, click Edit, then New, and add:%JAVA_HOME%\bin - Click OK on all dialogs.
macOS / Linux
Add the following to your shell profile (~/.zshrc, ~/.bashrc, or ~/.bash_profile):
export JAVA_HOME=/path/to/your/jdk
export PATH=$JAVA_HOME/bin:$PATH
Then reload the profile:
source ~/.zshrc
On macOS you can locate the JDK automatically:
export JAVA_HOME=$(/usr/libexec/java_home -v 21)
Verify the Installation
Open a new terminal or command prompt and run:
java -version
javac -version
You should see the version you installed. If the commands are not found, double-check the PATH entry and make sure you opened a new terminal so the changes take effect.
Step 3: Set Up VS Code
Visual Studio Code is a lightweight, free editor with excellent Java support through extensions.
Install VS Code
Download and install VS Code from the official website for your operating system.
Install the Java Extension Pack
- Open VS Code.
- Click the Extensions icon in the sidebar (or press
Ctrl+Shift+X). - Search for Extension Pack for Java (published by Microsoft).
- Click Install.
This pack bundles several essential extensions:
- Language Support for Java — code completion, navigation, and error checking.
- Debugger for Java — set breakpoints and step through code.
- Test Runner for Java — run and debug JUnit tests.
- Maven for Java and Gradle for Java — build tool integration.
- Project Manager for Java — manage projects and dependencies.
Point VS Code at Your JDK
VS Code usually detects JAVA_HOME automatically. To set it explicitly:
- Open the Command Palette (
Ctrl+Shift+P). - Search for Preferences: Open User Settings (JSON).
- Add a
java.jdt.ls.java.homeentry pointing to your JDK:{ "java.jdt.ls.java.home": "/path/to/your/jdk" }
Step 4: Write and Run Your First Program
- Create a folder for your project and open it in VS Code.
- Create a file named
Hello.java:public class Hello { public static void main(String[] args) { System.out.println("Hello, Java!"); } } - Click the Run button above the
mainmethod, or open a terminal and run:javac Hello.java java Hello
You should see Hello, Java! printed to the console.
Troubleshooting
java: command not found: ThePATHis not set correctly, or you did not open a new terminal after editing it.- Wrong Java version: Multiple JDKs may be installed. Check
JAVA_HOMEand the order ofPATHentries. - VS Code does not recognize Java: Reload the window (
Ctrl+Shift+P→ Developer: Reload Window) and confirm the Extension Pack for Java is installed and enabled.
With the JDK installed, the PATH configured, and VS Code ready to go, you now have a complete environment for learning and building Java applications.