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.

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 -version

If 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                      # CentOS

Step 2: Install OpenJDK

Ubuntu/Debian-based distributions:
sudo apt install openjdk-XX-jdk
Example: Installing OpenJDK 21:
sudo apt install openjdk-21-jdk
Fedora:
sudo dnf install java-XX-openjdk
CentOS/RHEL:
sudo yum install java-XX-openjdk
Replace 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 java
Example output:
/usr/lib/jvm/java-21-openjdk-amd64/bin/java
2. Open the .bashrc or .profile file:
nano ~/.bashrc
or
nano ~/.profile
3. Add the following lines at the end:
export JAVA_HOME=/usr/lib/jvm/java-21-openjdk-amd64
export PATH=$JAVA_HOME/bin:$PATH
4. Save and exit (CTRL + X, then Y and Enter).
5. Apply the changes:
source ~/.bashrc

Step 4: Verify Java Installation

Run the following command:
java -version
javac -version
This should display the installed Java version.

Install OpenJDK on macOS

Step 1: Check If Java Is Installed

macOS may come with an older version of Java. Check with:
java -version
If 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 openjdk
To 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 later
or
nano ~/.bash_profile  # For older macOS versions
3. Add the following lines:
export JAVA_HOME=$(/usr/libexec/java_home)
export PATH=$JAVA_HOME/bin:$PATH
4. Save and exit (CTRL + X, then Y and Enter).
5. Apply the changes:
source ~/.zshrc
or
source ~/.bash_profile

Step 4: Verify Java Installation

Check if Java is installed correctly:
java -version
javac -version
Index
Modern Java - What’s new in Java 9 to Java 17

32 min

Chapter 1: Introduction

3 min

Chapter 2: History of Java

9 min

Chapter 3: Features of Java

4 min

Chapter 4: How to Install Java?

2 min

Chapter 5: How to Set Up Java Path

2 min

Chapter 6: JDK, JVM and JRE

2 min

Chapter 7: First Java Program in Java

15 min

What is ClassLoader in Java ?

2 min

Object Oriented Programming (OOPs) Concept

17 min

Concurrency in Java: Creating and Starting a Thread

12 min

Concurrency in Java: Interrupting and Joining Threads

5 min

Concurrency in Java: Race condition, critical section, and atomic operations

13 min

Concurrency in Java: Reentrant, Read/Write and Stamped Locks

11 min

Concurrency in Java: "synchronized" and "volatile" keywords

10 min

Concurrency in Java: using wait(), notify() and notifyAll()

6 min

Concurrency in Java: What is "Semaphore" and its use?

2 min

Concurrency in Java: CompletableFuture and its use

18 min

Concurrency in Java: Producer-consumer problem using BlockingQueue

2 min

Concurrency in Java: Producer-Consumer Problem

2 min

Concurrency in Java: Thread pools, ExecutorService & Future

14 min

Java 8 Lambdas, Functional Interface & "static" and "default" methods

28 min

Method Reference in Java (Instance, Static, and Constructor Reference)

9 min

What's new in Java 21: A Tour of its Most Exciting Features

14 min

Java Memory Leaks & Heap Dumps (Capturing & Analysis)

9 min

Memory footprint of the JVM (Heap & Non-Heap Memory)

15 min