Master Code
On The Go

Learn. Practice. Build.

How-To Guide

How to Install Java

Java powers Android apps, enterprise backends, and Minecraft servers. Here's how to install the JDK on any operating system in about 10 minutes.

📋 What You'll Need

✅ Java (OpenJDK) is completely free. No credit card, no signup.

🧩 JDK vs JRE vs JVM — What Do You Need?

If you're learning or building, install the JDK.

1

Download the JDK

Go to the official OpenJDK distribution site:

👉 adoptium.net

Click Latest LTS Release and download the installer for your OS. Install Java 21 LTS — it's supported until 2031 and works with all modern tools.

  • Windows: .msi installer (x64)
  • Mac: .pkg installer (Intel or Apple Silicon)
  • Linux: .deb, .rpm, or use your package manager
2

Run the Installer

Windows:

  • Open the downloaded .msi file
  • Click "Next" through the wizard
  • On "Custom Setup", enable "Set JAVA_HOME variable" and "Add to PATH"
  • Click "Install" → "Finish"

Mac:

  • Open the downloaded .pkg file
  • Follow the installer wizard
  • Or via Homebrew: brew install openjdk@21

Linux (Ubuntu/Debian):

sudo apt update
sudo apt install openjdk-21-jdk
3

Verify the Installation

Open a terminal (Command Prompt, Terminal, or PowerShell) and run:

java --version
javac --version

You should see version numbers like openjdk 21.0.1. If both commands work, Java is installed correctly.

4

Test with Hello World

Create a file called Hello.java:

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

Compile and run it:

javac Hello.java
java Hello

You should see Java is working! printed. 🎉

💡 How Java works: javac compiles your source code into bytecode (Hello.class). Then java runs that bytecode on the JVM. That's the two-step process.

⚙️ Setting JAVA_HOME (If Needed)

Some tools (Maven, Gradle, Android Studio) look for a JAVA_HOME environment variable. Set it only if a tool asks for it.

macOS / Linux — add to ~/.zshrc or ~/.bashrc:

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

Windows PowerShell:

[Environment]::SetEnvironmentVariable("JAVA_HOME", "C:\Program Files\Eclipse Adoptium\jdk-21.0.1.12-hotspot", "Machine")

🔧 Troubleshooting

"java: command not found"

Your PATH doesn't include Java's bin folder. Restart your terminal. On macOS/Linux, check that JAVA_HOME is exported correctly.

"javac: command not found" (but java works)

You installed the JRE instead of the JDK. The JRE runs Java apps but can't compile them. Uninstall it and install the JDK.

Multiple Java versions installed

Run java --version to see which is active. On macOS use /usr/libexec/java_home -V to list all. On Linux use update-alternatives --config java.

🎯 What's Next?

Now that you have Java installed, you're ready to start building:

← All How-To Guides