Before you write a single line of Java, you need a working toolchain on your machine. Ninety percent of the "Java won't run" messages we get from new students trace back to one of two things: no JDK installed, or a JDK installed but not on the PATH. This guide fixes both, on all three major operating systems.
We will install the JDK, set the environment variables, and verify the result. If you are still unsure what the JDK even is, read JDK vs JRE vs JVM first, then come back here to actually put it on your computer.
Pick the right JDK before you download
The single most common beginner mistake is downloading the JRE instead of the JDK, or grabbing a random version. Get this decision right and the rest is mechanical.
- You need the JDK, not the JRE. The JDK contains the
javaccompiler that turns your.javasource into bytecode. The JRE only runs already-compiled programs. As a learner you compile constantly, so the JRE alone is useless to you. - Choose a Long-Term Support (LTS) version. In 2026 that means Java 17 or Java 21. LTS releases get years of updates and match what real employers run.
- Free OpenJDK builds are fine. Adoptium Temurin, Amazon Corretto, and Azul Zulu are all free OpenJDK builds. Oracle's own JDK works too; just be aware its license has conditions for some production scenarios.
Pro tip: Stick to one JDK version while you are learning. Juggling Java 8, 17, and 21 on the same machine is a classic source of confusing errors. Add more versions later, once you understand how
JAVA_HOMEselects between them.
Install Java on Windows
Windows needs the most manual setup because it does not configure environment variables for you.
Step 1 — Download and run the installer. Get the .msi installer for an LTS JDK and run it. Accept the defaults; note the install folder, usually something like C:\Program Files\Eclipse Adoptium\jdk-21.
Step 2 — Set JAVA_HOME. Open Edit the system environment variables → Environment Variables. Under System variables, click New:
- Variable name:
JAVA_HOME - Variable value: the JDK folder, e.g.
C:\Program Files\Eclipse Adoptium\jdk-21
Step 3 — Add Java to the PATH. In the same dialog, edit the Path variable and add a new entry:
%JAVA_HOME%\bin
Step 4 — Verify in a fresh terminal. Close every open Command Prompt or PowerShell window, then open a new one. Environment changes only apply to terminals opened after the change.
java -version
javac -version
Both should print a version number. If java works but javac does not, you installed a JRE, not a JDK — go back and install the JDK.
Common mistake: Editing
Pathand then testing in a terminal that was already open. The old terminal still holds the old environment. Always open a new window before you runjava -version.
Install Java on macOS
macOS gives you two clean routes.
Option A — Homebrew (recommended). If you have the Homebrew package manager, one command handles the download, install, and most of the wiring:
brew install --cask temurin
Option B — the .pkg installer. Download the macOS .pkg for an LTS JDK and double-click it. It installs into /Library/Java/JavaVirtualMachines/.
Set JAVA_HOME using the built-in helper, which always points at your current JDK:
echo 'export JAVA_HOME=$(/usr/libexec/java_home)' >> ~/.zshrc
source ~/.zshrc
java -version
The /usr/libexec/java_home tool is a macOS convenience that resolves the active JDK path for you, so you never hard-code a version folder. If you use the older Bash shell, put the same line in ~/.bash_profile instead of ~/.zshrc.
Install Java on Linux
On most distributions the package manager is the fastest path.
Debian / Ubuntu:
sudo apt update
sudo apt install openjdk-21-jdk
java -version
Fedora / RHEL:
sudo dnf install java-21-openjdk-devel
java -version
The -devel (or -jdk) package is important — the plain java package on some distros is only the runtime. If javac is missing after install, you grabbed the runtime-only package.
To set JAVA_HOME persistently, find the real install path and export it in your shell profile:
readlink -f $(which java) # prints the real java install path
export JAVA_HOME=/usr/lib/jvm/java-21-openjdk-amd64
export PATH=$JAVA_HOME/bin:$PATH
Put the two export lines (with your actual path) into ~/.bashrc, run source ~/.bashrc to reload, then confirm with java -version. The readlink -f trick resolves the symbolic links most distributions use, so you set JAVA_HOME to the concrete JDK folder rather than a symlink that could later point elsewhere.
Verify the install with a real compile
java -version proves the runtime works, but it does not prove you can compile. Do a full round trip so you catch a missing compiler now instead of during your first Java program.
Create a file called Check.java:
public class Check {
public static void main(String[] args) {
System.out.println("JDK is installed and working");
}
}
Then compile and run it from the folder that contains the file:
javac Check.java # produces Check.class
java Check # prints the message
If you see JDK is installed and working, your toolchain is complete: compiler, runtime, and PATH are all correct. If javac throws "command not found," your PATH points at a JRE or nothing at all — recheck the bin folder in JAVA_HOME.
For a slightly stronger check, compile a program that reports the runtime version from inside Java itself. This confirms the running JVM matches the version you think you installed, which is useful when several JDKs are present:
public class VersionCheck {
public static void main(String[] args) {
System.out.println("Running on Java " +
System.getProperty("java.version"));
System.out.println("JVM home: " +
System.getProperty("java.home"));
}
}
Compile with javac VersionCheck.java and run java VersionCheck. The java.version line should match your java -version output, and java.home shows the exact JDK the JVM launched from — the fastest way to catch a stray older install winning on the PATH.
Pro tip: The class you compile with
javac Check.javabecomesCheck.class. When you run it, typejava Checkwith no.classextension. Passingjava Check.classis a frequent beginner error that produces a confusing "could not find or load main class" message.
Common setup problems and fixes
A few failures show up again and again in our beginner batches:
- "java is not recognized" / "command not found": the
binfolder is not on the PATH, or you did not reopen the terminal. RecheckJAVA_HOMEand%JAVA_HOME%\bin. java -versionshows an old version you thought you removed: an older JDK is earlier on the PATH. Fix the PATH order or updateJAVA_HOMEto the version you want as default.javacmissing butjavaworks: you installed a JRE or a runtime-only Linux package. Install the JDK /-develpackage.- IDE uses a different version than the terminal: IntelliJ and Eclipse each store their own "Project SDK" setting. Point the IDE at the same
JAVA_HOMEso builds match. - Two JDKs fight on Windows: some installers add their own entry to the front of
Path. If the wrong version wins, move%JAVA_HOME%\binabove the others in thePathlist so it is found first.
The mental model that prevents most of these problems is simple. JAVA_HOME tells other tools — Maven, Gradle, your IDE — which JDK to use, while the PATH entry is what lets you type java and javac in any folder without the full path. When both point at the same LTS JDK, every tool on your machine agrees on the version, and confusing "works in the IDE but not the terminal" bugs disappear.
Understanding why these variables matter also helps you in interviews. Knowing the difference between the JDK, JRE, and JVM and how the JVM finds and loads classes is exactly the kind of fundamentals question freshers get asked in their first technical round.
What to do next
With Java installed and verified, you are ready to write and run real code. Head straight to Your First Java Program to compile Hello World and understand every line of it, then start on variables and data types to build from there.
If you would rather learn with a structured path and a mentor checking your setup and your code, that is how we run the first week of the Java Full Stack course — everyone gets the same environment working before we touch syntax, so nobody is stuck debugging a PATH on day three. You can also browse the full Java learning hub to see how installation fits into the wider roadmap.
Frequently Asked Questions
Do I need the JDK or the JRE to start learning Java?
Which Java version should a beginner install in 2026?
Is OpenJDK the same as Oracle JDK?
Why does my terminal say 'java is not recognized' after installing?
Do I need to install Java before installing an IDE like IntelliJ or Eclipse?
Want to Build Your Career in Java Full Stack with AI?
Join CodeBegun and train with working industry engineers — Explore the Program

