Chapter 4: How to Install Java?
- 4.1/5
- 13
- Mar 11, 2025
OpenJDK (Open Java Development Kit) is a free and open-source implementation of the Java programming language. It is widely used for development and production environments.
This guide provides step-by-step instructions to install OpenJDK on Windows, Linux, and macOS.
- Alternatively, use https://adoptium.net (Eclipse Temurin)
2. Select the latest JDK version for Windows.
3. Download the .zip file (no installer required).
2. Rename the folder to a shorter name, e.g., C:\Java\jdk-XX.
1. Open Environment Variables:
- Press Win + R, type sysdm.cpl, and hit Enter.
- Go to the Advanced tab → Click Environment Variables.
This guide provides step-by-step instructions to install OpenJDK on Windows, Linux, and macOS.
Install OpenJDK on Windows
Step 1: Download OpenJDK
1. Visit the OpenJDK download page: - https://jdk.java.net (Official OpenJDK builds)- Alternatively, use https://adoptium.net (Eclipse Temurin)
2. Select the latest JDK version for Windows.
3. Download the .zip file (no installer required).
Step 2: Extract OpenJDK
1. Extract the downloaded .zip file to a preferred directory (e.g., C:\Java\).2. Rename the folder to a shorter name, e.g., C:\Java\jdk-XX.
Step 3: Set Up Java Environment Variables
To run Java globally, set the JAVA_HOME and Path variables.1. Open Environment Variables:
- Press Win + R, type sysdm.cpl, and hit Enter.
- Go to the Advanced tab → Click Environment Variables.
2. Set JAVA_HOME:
- Click New under System Variables.
- Variable Name: JAVA_HOME
- Variable Value: C:\Java\jdk-XX (your OpenJDK path).
- Click OK.
3. Add Java to PATH:
- Select Path under System Variables, click Edit → New.
- Add: C:\Java\jdk-XX\bin.
- Click OK → Restart your computer.
Step 4: Verify Java Installation
Open Command Prompt (cmd) and run:java -version javac -versionIf Java is installed correctly, it will display the version details.
Install OpenJDK on Linux
Step 1: Update System Packages
Before installing Java, update the package repository:sudo apt update && sudo apt upgrade # Ubuntu/Debian sudo dnf update # Fedora sudo yum update # CentOSStep 2: Install OpenJDK
Ubuntu/Debian-based distributions:sudo apt install openjdk-XX-jdkExample: Installing OpenJDK 21:sudo apt install openjdk-21-jdkFedora:sudo dnf install java-XX-openjdkCentOS/RHEL:sudo yum install java-XX-openjdkReplace XX with the required version (e.g., 21 for Java 21).
Step 3: Set JAVA_HOME (Optional)
To set the Java environment variable:
1. Find the Java installation path:sudo update-alternatives --config javaExample output:/usr/lib/jvm/java-21-openjdk-amd64/bin/java2. Open the .bashrc or .profile file:nano ~/.bashrcornano ~/.profile3. Add the following lines at the end:export JAVA_HOME=/usr/lib/jvm/java-21-openjdk-amd64 export PATH=$JAVA_HOME/bin:$PATH4. Save and exit (CTRL + X, then Y and Enter).
5. Apply the changes:source ~/.bashrcStep 4: Verify Java Installation
Run the following command:java -version javac -version
This should display the installed Java version.
2. Edit the shell configuration file:
5. Apply the changes:
Install OpenJDK on macOS
Step 1: Check If Java Is Installed
macOS may come with an older version of Java. Check with:java -versionIf Java is not found, proceed with installation.
Step 2: Install OpenJDK Using Homebrew (Recommended)
Homebrew is a package manager for macOS. Install OpenJDK with:brew install openjdkTo install a specific Java version (e.g., Java 21):
brew install openjdk@21
Step 3: Set JAVA_HOME (Optional)
1. Open Terminal.2. Edit the shell configuration file:
nano ~/.zshrc # For macOS Catalina and lateror
nano ~/.bash_profile # For older macOS versions3. Add the following lines:
export JAVA_HOME=$(/usr/libexec/java_home) export PATH=$JAVA_HOME/bin:$PATH4. Save and exit (CTRL + X, then Y and Enter).
5. Apply the changes:
source ~/.zshrcor
source ~/.bash_profile
Step 4: Verify Java Installation
Check if Java is installed correctly:java -version javac -version