Install Java JDK on Windows, macOS, and Linux
Every Java journey begins with a working JDK. The Java Development Kit provides the compiler (javac), runtime tools, and the JRE—everything you need to build and run Java applications. This guide walks you through installing a JDK on Windows, macOS, and Linux, configuring the essential environment variables, and verifying that your setup is correct.
Choose a current Long‑Term Support (LTS) release unless your project dictates otherwise. At the time of writing, JDK 21 and JDK 17 are the recommended LTS versions for production use. The steps below apply to any modern JDK distribution.
1. What You Need Before Installing​
- Administrative access – required to install system‑wide packages or modify system paths on some operating systems.
- Stable internet connection – to download the JDK distribution.
- A terminal or command prompt – for running verification commands.
- A decision on which Java version – prefer JDK 21 or 17 unless a specific project requires an older or newer version.
2. Choose a JDK Distribution​
We recommend using Eclipse Temurin (an OpenJDK distribution) as the default JDK for learning and development environments. If your project requirements, organizational policies, or commercial support needs require Oracle JDK, you may install Oracle JDK directly.
Several reliable distributions are available. The code you write and run is the same; the differences lie in licensing, update cadence, and platform support.
| Distribution | Provider | License | Notes |
|---|---|---|---|
| Oracle JDK | Oracle | Oracle No‑Fee Terms (JDK 17+) | Free for production; older versions may require a license. |
| Eclipse Temurin | Eclipse Adoptium | OpenJDK (GPL + Classpath) | Community‑driven, widely adopted, TCK‑tested. |
| Amazon Corretto | Amazon | GPL + Classpath | Free, long‑term support, AWS optimized. |
| Azul Zulu | Azul | GPL + Classpath | Broad platform support, commercial support available. |
| OpenJDK (upstream) | OpenJDK community | GPL + Classpath | Reference implementation, no commercial support. |
Recommendation for most developers: Eclipse Temurin or Amazon Corretto. They are free, TCK‑tested, and receive regular security updates. For corporate environments, check your organization’s policies.
3. Install Java on Windows​
-
Download the JDK
Visit the Eclipse Temurin or your chosen distribution’s site, such as Oracle JDK. Select the.msiinstaller for Windows. -
Run the installer
Launch the.msifile. Accept the license agreement. Keep the default installation directory (e.g.,C:\Program Files\Eclipse Adoptium\jdk-21.0.4.7-hotspot\) or change it if needed. -
Set
JAVA_HOME- Press Windows + Pause/Break → Advanced system settings → Environment Variables.
- Under System variables, click New.
- Variable name:
JAVA_HOME - Variable value: the JDK installation path (e.g.,
C:\Program Files\Eclipse Adoptium\jdk-21.0.4.7-hotspot). - Click OK.
-
Update
PATH- In the same Environment Variables window, select the
Pathvariable and click Edit. - Click New and add
%JAVA_HOME%\bin. - Click OK to close all dialogs.
- In the same Environment Variables window, select the
-
Verify
Open Command Prompt or PowerShell and run:java -versionjavac -versionecho %JAVA_HOME%You should see version information for both
javaandjavac, andJAVA_HOMEshould point to the JDK directory.
4. Install Java on macOS​
-
Download the JDK
Download the.pkginstaller from your chosen distribution’s website (e.g., Eclipse Temurin for macOS). -
Install
Double‑click the.pkgand follow the prompts. The JDK is typically installed in/Library/Java/JavaVirtualMachines/. -
Configure environment variables
macOS useszshby default. Edit your shell profile:nano ~/.zshrcAdd the following lines (adjust the version path if needed):
export JAVA_HOME=$(/usr/libexec/java_home -v 21)export PATH=$JAVA_HOME/bin:$PATHIf you use
bash, edit~/.bash_profileinstead. -
Apply changes
source ~/.zshrc -
Verify
java -versionjavac -versionecho $JAVA_HOMEThe output should show the installed JDK version.
macOS note: The
/usr/libexec/java_homeutility helps manage multiple JDKs. You can list all installed versions with/usr/libexec/java_home -V.
5. Install Java on Linux​
The exact commands depend on your distribution and whether you prefer a package manager or a manual tarball.
Option A: Package Manager (Recommended)​
Debian/Ubuntu (apt)
sudo apt update
sudo apt install temurin-21-jdk
If the Temurin repository is not configured, follow the Adoptium installation instructions to add it.
Fedora/RHEL/CentOS (dnf/yum)
# For Fedora
sudo dnf install temurin-21-jdk
# For older RHEL/CentOS
sudo yum install temurin-21-jdk
Arch Linux (pacman)
sudo pacman -S jdk-openjdk # or a specific version like jdk21-openjdk
Option B: Manual Tarball​
-
Download the
.tar.gzarchive from your distribution’s site. -
Extract:
tar -xzf jdk-21_linux-x64_bin.tar.gzsudo mv jdk-21.0.2 /usr/lib/jvm/ -
Configure environment variables in
~/.bashrcor~/.zshrc:export JAVA_HOME=/usr/lib/jvm/jdk-21.0.2export PATH=$JAVA_HOME/bin:$PATH -
Source the profile and verify.
Verification (all Linux methods)​
java -version
javac -version
echo $JAVA_HOME
If multiple JDKs are installed, use update-alternatives --config java (Debian/Ubuntu) or manually symlink the desired version.
6. Configure Environment Variables​
JAVA_HOME– points to the JDK installation root. Many tools (Maven, Gradle, IDE) rely on it to locate the JDK.PATH– must include$JAVA_HOME/bin(or%JAVA_HOME%\binon Windows) so thatjava,javac, and other binaries are accessible from any terminal.- Why it matters – without proper variables, build tools may fail, and the wrong Java version might be picked up. Consistent configuration prevents “works on my machine” issues.
7. Verify the Installation​
Beyond the basic version checks, ensure your environment is fully functional:
-
Compile and run a test program
CreateHelloWorld.java:public class HelloWorld {public static void main(String[] args) {System.out.println("JavaDevPro is ready.");}}Compile and run:
javac HelloWorld.javajava HelloWorldYou should see the printed message.
-
Check for conflicting installations
Runwhere java(Windows) orwhich java(macOS/Linux). The path should include the JDK you just installed. -
If the wrong version appears
Reorder thePATHentries so the desired JDK’sbindirectory appears first. On Linux/macOS, you can also usesudo update-alternativesorsdkmanto manage multiple JDKs.
8. Common Installation Problems​
| Symptom | Probable Cause | Solution |
|---|---|---|
java command not found | PATH does not include JDK bin. | Add %JAVA_HOME%\bin (Windows) or $JAVA_HOME/bin (macOS/Linux) to your PATH. |
javac not found, but java works | JRE installed instead of JDK. | Install a full JDK, not just a JRE. |
JAVA_HOME not set | Variable not created or not exported. | Create the variable and restart the terminal or source your shell profile. |
| Wrong version appears | Another Java is higher in PATH. | Reorder PATH or use absolute paths. |
| Permission denied (Linux/macOS) | Installation directory not writable. | Use sudo or install to a user‑writable location. |
| Installer fails on Windows | Lack of admin privileges. | Right‑click the installer and select “Run as administrator”. |
| Multiple JDK installations conflict | Several JDKs on PATH or JAVA_HOME mismatch. | Uninstall unwanted versions, use a version manager (sdkman), or set JAVA_HOME explicitly. |
9. Quick Reference Table​
| Platform | Main Steps | Verification Commands |
|---|---|---|
| Windows | Download .msi, install, set JAVA_HOME, add bin to PATH | java -version, javac -version, echo %JAVA_HOME% |
| macOS | Download .pkg, install, update ~/.zshrc, source it | java -version, javac -version, echo $JAVA_HOME |
| Linux | Use package manager or tarball, set environment variables | java -version, javac -version, echo $JAVA_HOME |
10. Recommended Next Steps​
Your Java environment is ready. Now you can:
- Follow the Java Learning Roadmap to plan your journey.
- Dive into Java Foundations to start writing real Java code.
- Read What Is JavaDevPro and How to Use This Handbook to get the most from this resource.
With the JDK installed, you have taken the first concrete step toward professional Java engineering. Keep this guide handy whenever you set up a new machine or onboard a colleague.