Install Java & Setup
Before you write a single line of Java, you need two things installed on your computer:
- ›The JDK — to compile and run Java code
- ›A code editor or IDE — to write Java code comfortably
This page walks you through both, step by step, for Windows, Mac, and Linux.
By the end, you'll have Java installed, verified, and ready — and you'll run your first program in the terminal.
👉 What you'll set up:
- ›JDK 21 (the latest LTS version — free, stable, widely supported)
- ›A code editor (VS Code recommended for beginners, IntelliJ IDEA for professionals)
- ›Your first Java program running in the terminal
🧘 Never installed a programming language before? That's fine.
This guide assumes zero experience with development environments. Every step is explained. If something goes wrong, the troubleshooting section at the bottom covers the most common issues.
Step 0 — Which JDK Should You Install?
Before downloading anything, let's answer the two questions every beginner has:
Which version?
Install Java 21 — the latest Long-Term Support (LTS) release. LTS versions receive security updates for years and are what companies actually use in production. Avoid non-LTS versions (like Java 22 or 23) for learning — they expire quickly.
Which distribution?
Java has multiple free distributions. They are all compatible — the difference is who builds and maintains them.
| Distribution | Who Maintains It | Best For |
|---|---|---|
| Eclipse Temurin (OpenJDK) | Eclipse Foundation | ✅ Recommended for most |
| Oracle JDK | Oracle | Fine for development |
| Amazon Corretto | Amazon | Cloud / AWS projects |
| Microsoft Build of OpenJDK | Microsoft | Azure / Windows projects |
Use Eclipse Temurin (OpenJDK). It is free, open-source, widely used, and has no licensing complications. The examples below use Temurin — but any distribution of Java 21 works identically.
Install on Windows
Step 1 — Download the JDK
Go to adoptium.net and download:
- ›Version: Java 21 (LTS)
- ›OS: Windows
- ›Architecture: x64 (for most modern Windows computers)
Download the .msi installer file.
Step 2 — Run the Installer
Double-click the .msi file. The installation wizard opens.
On the Custom Setup screen, make sure these two options are checked:
- ›✅ Add to PATH — this lets you run
javaandjavacfrom any terminal - ›✅ Set JAVA_HOME variable — required by many build tools (Maven, Gradle)
Click Next → Install → Finish.
Step 3 — Verify the Installation
Open Command Prompt (search "cmd" in the Start menu) and run:
1java -versionYou should see something like:
1openjdk version "21.0.3" 2024-04-16
2OpenJDK Runtime Environment Temurin-21.0.3+9 (build 21.0.3+9)
3OpenJDK 64-Bit Server VM Temurin-21.0.3+9 (build 21.0.3+9, mixed mode, sharing)Then verify the compiler:
1javac -versionOutput:
1javac 21.0.3If both commands show version numbers, Java is installed correctly. Skip to Set Up Your Editor.
⚠️ If you get "java is not recognized": The PATH was not set correctly. Go to Control Panel → System → Advanced System Settings → Environment Variables. Under System Variables, find Path, click Edit, and add the path to your JDK's bin folder (e.g. C:\Program Files\Eclipse Adoptium\jdk-21.0.3.9-hotspot\bin).
Install on macOS
Step 1 — Download the JDK
Option A — Homebrew (recommended if you have it):
Open Terminal and run:
1brew install --cask temurin@21Homebrew handles everything automatically, including PATH setup.
Option B — Manual download:
Go to adoptium.net and download:
- ›Version: Java 21 (LTS)
- ›OS: macOS
- ›Architecture: aarch64 for Apple Silicon (M1/M2/M3) or x64 for Intel Mac
Download the .pkg installer.
Step 2 — Run the Installer (Manual only)
Double-click the .pkg file and follow the prompts. Click through Continue → Agree → Install. macOS may ask for your password.
Step 3 — Verify the Installation
Open Terminal (search "Terminal" in Spotlight) and run:
1java -versionExpected output:
1openjdk version "21.0.3" 2024-04-16
2OpenJDK Runtime Environment Temurin-21.0.3+9 (build 21.0.3+9)Verify the compiler:
1javac -versionOutput:
1javac 21.0.3⚠️ Apple Silicon (M1/M2/M3) users: Make sure you downloaded the aarch64 version, not x64. Using the wrong architecture causes the JVM to run under Rosetta emulation and is significantly slower.
⚠️ If Terminal shows the old system Java: macOS ships with an outdated Apple Java. Run which java — if it shows /usr/bin/java, your PATH hasn't updated. Close Terminal, reopen it, and try again. If the issue persists, add this to your ~/.zshrc file:
1export JAVA_HOME=$(/usr/libexec/java_home -v 21)
2export PATH=$JAVA_HOME/bin:$PATHInstall on Linux
Ubuntu / Debian
Open Terminal and run:
1sudo apt update
2sudo apt install temurin-21-jdkIf the temurin package isn't found, add the Adoptium repository first:
1wget -O - https://packages.adoptium.net/artifactory/api/gpg/key/public | sudo apt-key add -
2echo "deb https://packages.adoptium.net/artifactory/deb $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/adoptium.list
3sudo apt update
4sudo apt install temurin-21-jdkFedora / RHEL / CentOS
1sudo dnf install java-21-openjdk-develVerify on any Linux
1java -version
2javac -versionBoth should show version 21.
💡 Multiple Java versions: If you have multiple Java versions installed, use sudo update-alternatives --config java (Ubuntu/Debian) or sudo alternatives --config java (Fedora) to switch between them.
Set Up Your Editor
You can write Java in any text editor — even Notepad. But a proper editor gives you syntax highlighting, error detection, auto-completion, and debugging tools that save hours every week.
Option A — VS Code (Recommended for Beginners)
VS Code is free, lightweight, and works well for Java when set up correctly.
Install VS Code: Download from code.visualstudio.com and install it.
Install the Java Extension Pack:
Open VS Code → click the Extensions icon (or press Ctrl+Shift+X) → search "Extension Pack for Java" → click Install.
This installs six extensions including:
- ›Language Support for Java
- ›Debugger for Java
- ›Test Runner for Java
- ›Maven for Java
Once installed, VS Code detects your JDK automatically and gives you full Java support.
Option B — IntelliJ IDEA (Recommended for Professionals)
IntelliJ IDEA is the most powerful Java IDE available. The Community Edition is completely free.
Download from jetbrains.com/idea — select Community Edition.
IntelliJ automatically detects installed JDKs, provides the best code completion in the industry, and is used by developers at Google, Amazon, and most large tech companies.
💡 Which should you choose? If you are just starting — VS Code. It's lighter and simpler. If you are serious about Java as a career — IntelliJ IDEA Community. The professional tooling makes a real difference once you start writing larger programs.
Write and Run Your First Program
Now let's confirm everything works end-to-end.
Step 1 — Create the file
Create a new folder called JavaProjects anywhere on your computer. Inside it, create a file called HelloWorld.java.
Open the file in your editor and type this exactly:
1public class HelloWorld {
2 public static void main(String[] args) {
3 System.out.println("Hello, World!");
4 }
5}⚠️ The filename and class name must match exactly. The file is HelloWorld.java and the class is HelloWorld. If they differ, the compiler throws an error.
Step 2 — Compile
Open your terminal, navigate to your JavaProjects folder:
1cd path/to/JavaProjectsCompile the file:
1javac HelloWorld.javaIf the command runs with no output, compilation succeeded. You'll see a new file called HelloWorld.class appear in the folder — that's your Bytecode.
If you see an error, check that your code matches exactly what is shown above — every letter, every semicolon, every brace.
Step 3 — Run
Now run the compiled program:
1java HelloWorldYou should see:
1Hello, World!That's it. Java is installed, set up, and working.
💡 Notice: You run java HelloWorld — not java HelloWorld.class. The java command takes the class name, not the filename.
Common Installation Problems
| Problem | Likely Cause | Fix |
|---|---|---|
java is not recognized (Windows) | PATH not set | Add JDK bin folder to System PATH in Environment Variables |
command not found: java (Mac/Linux) | JDK not in PATH | Add export PATH=$JAVA_HOME/bin:$PATH to your shell config file |
javac: command not found | JRE installed instead of JDK | Uninstall and reinstall the full JDK (not JRE) |
error: class HelloWorld is public, should be in a file named HelloWorld.java | Filename doesn't match class name | Rename the file to match the class name exactly |
error: reached end of file while parsing | Missing closing brace } | Count your opening { and closing } — they must match |
| Old Java version shows after install | Multiple JDKs installed | Set JAVA_HOME to point to Java 21 and update PATH |
Verify Your Full Setup
Run through this quick checklist to confirm everything is ready:
- ›
java -versionshows Java 21 ✅ - ›
javac -versionshows javac 21 ✅ - ›
HelloWorld.javacompiled without errors ✅ - ›
java HelloWorldprinted "Hello, World!" ✅ - ›Your editor opens
.javafiles with syntax highlighting ✅
If all five pass, your environment is fully set up and you are ready to learn Java.
The 3 Things to Remember From This Page
- ›Always install the JDK — not the JRE. The JDK includes the compiler (
javac) and everything you need as a developer - ›Use Java 21 LTS — stable, widely supported, and what most companies use in production
- ›The filename must match the class name —
HelloWorld.javamust containpublic class HelloWorld. This is one of the most common beginner mistakes
Summary
- ›Install JDK 21 (Eclipse Temurin) — free, stable, and recommended for all platforms
- ›On Windows: use the
.msiinstaller and check "Add to PATH" during setup - ›On macOS: use Homebrew (
brew install --cask temurin@21) or the.pkginstaller - ›On Linux: use your package manager (
apt,dnf) or the Adoptium repository - ›Verify with
java -versionandjavac -version— both should show version 21 - ›Use VS Code + Extension Pack for Java (beginners) or IntelliJ IDEA Community (professionals)
- ›Compile with
javac HelloWorld.java→ run withjava HelloWorld
What to Read Next
| Topic | Link |
|---|---|
| Write and understand your first Java program | First Java Program → |
| How Java compiles and runs internally | How Java Works → |
| JDK, JRE, and JVM — what each one does | JDK, JRE & JVM → |
| Start writing Java — syntax and structure | Syntax & Structure → |