Java Tutorial
🔍

Install Java & Setup

Before you write a single line of Java, you need two things installed on your computer:

  1. The JDK — to compile and run Java code
  2. 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.

DistributionWho Maintains ItBest For
Eclipse Temurin (OpenJDK)Eclipse Foundation✅ Recommended for most
Oracle JDKOracleFine for development
Amazon CorrettoAmazonCloud / AWS projects
Microsoft Build of OpenJDKMicrosoftAzure / 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 java and javac from any terminal
  • Set JAVA_HOME variable — required by many build tools (Maven, Gradle)

Click NextInstallFinish.

Step 3 — Verify the Installation

Open Command Prompt (search "cmd" in the Start menu) and run:

Terminal
1java -version

You 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:

Terminal
1javac -version

Output:

1javac 21.0.3

If 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:

Terminal
1brew install --cask temurin@21

Homebrew 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 ContinueAgreeInstall. macOS may ask for your password.

Step 3 — Verify the Installation

Open Terminal (search "Terminal" in Spotlight) and run:

Terminal
1java -version

Expected 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:

Terminal
1javac -version

Output:

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:

Terminal
1export JAVA_HOME=$(/usr/libexec/java_home -v 21) 2export PATH=$JAVA_HOME/bin:$PATH

Install on Linux

Ubuntu / Debian

Open Terminal and run:

Terminal
1sudo apt update 2sudo apt install temurin-21-jdk

If the temurin package isn't found, add the Adoptium repository first:

Terminal
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-jdk

Fedora / RHEL / CentOS

Terminal
1sudo dnf install java-21-openjdk-devel

Verify on any Linux

Terminal
1java -version 2javac -version

Both 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:

Java
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:

Terminal
1cd path/to/JavaProjects

Compile the file:

Terminal
1javac HelloWorld.java

If 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:

Terminal
1java HelloWorld

You 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

ProblemLikely CauseFix
java is not recognized (Windows)PATH not setAdd JDK bin folder to System PATH in Environment Variables
command not found: java (Mac/Linux)JDK not in PATHAdd export PATH=$JAVA_HOME/bin:$PATH to your shell config file
javac: command not foundJRE installed instead of JDKUninstall and reinstall the full JDK (not JRE)
error: class HelloWorld is public, should be in a file named HelloWorld.javaFilename doesn't match class nameRename the file to match the class name exactly
error: reached end of file while parsingMissing closing brace }Count your opening { and closing } — they must match
Old Java version shows after installMultiple JDKs installedSet 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 -version shows Java 21 ✅
  • javac -version shows javac 21 ✅
  • HelloWorld.java compiled without errors ✅
  • java HelloWorld printed "Hello, World!" ✅
  • Your editor opens .java files 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

  1. Always install the JDK — not the JRE. The JDK includes the compiler (javac) and everything you need as a developer
  2. Use Java 21 LTS — stable, widely supported, and what most companies use in production
  3. The filename must match the class nameHelloWorld.java must contain public 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 .msi installer and check "Add to PATH" during setup
  • On macOS: use Homebrew (brew install --cask temurin@21) or the .pkg installer
  • On Linux: use your package manager (apt, dnf) or the Adoptium repository
  • Verify with java -version and javac -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 with java HelloWorld

What to Read Next

TopicLink
Write and understand your first Java programFirst Java Program →
How Java compiles and runs internallyHow Java Works →
JDK, JRE, and JVM — what each one doesJDK, JRE & JVM →
Start writing Java — syntax and structureSyntax & Structure →
Install Java & Setup | DevStackFlow