Skip to main content
โšก Calmops

Linux JDK Installation: Complete Guide

Installing JDK on Linux

This guide covers downloading and installing Oracle JDK on Linux. Note: Oracle JDK requires a license for commercial use; consider OpenJDK for free alternatives.

Introduction

Java Development Kit (JDK) is essential for developing and running Java applications. Linux offers multiple ways to install JDK, from package managers to manual installation. This guide covers the most reliable methods for getting Java working on your Linux system.

Choosing the right JDK distribution depends on your needs. OpenJDK is free and open source, suitable for most development and production use. Oracle JDK offers additional features and support but requires licensing for commercial use. Other distributions like Eclipse Temurin (formerly AdoptOpenJDK) provide well-tested builds of OpenJDK.

Ubuntu/Debian

# Update package index
sudo apt update

# Install OpenJDK 21 (latest LTS)
sudo apt install openjdk-21-jdk

# Install OpenJDK 17 (LTS, widely supported)
sudo apt install openjdk-17-jdk

# Install OpenJDK 11 (older LTS)
sudo apt install openjdk-11-jdk

CentOS/RHEL/Fedora

# Using yum/dnf
sudo yum install java-21-openjdk-devel

# Or specific version
sudo yum install java-17-openjdk-devel

Arch Linux

sudo pacman -S jdk21-openjdk
# Or LTS version
sudo pacman -S jdk17-openjdk

Method 2: Manual Installation from Tarball

Downloading JDK

Visit Oracle JDK Downloads or Eclipse Temurin for OpenJDK builds.

For example, download Oracle JDK:

wget https://download.oracle.com/java/21/latest/jdk-21_linux-x64_bin.tar.gz

Or OpenJDK from Eclipse Temurin:

wget https://github.com/adoptium/temurin21-binaries/releases/download/jdk-21.0.2%2B13/OpenJDK21U-jdk_x64_linux_hotspot_21.0.2_13.tar.gz

Extracting JDK

# Extract to /usr/local/
sudo tar -zxf jdk-21_linux-x64_bin.tar.gz -C /usr/local/

# Verify extraction
ls /usr/local/
# You should see: jdk-21.0.2

Configuring Environment Variables

Create a script in /etc/profile.d/ for system-wide variables:

sudo nano /etc/profile.d/java.sh

Add the following (adjust version number):

export JAVA_HOME=/usr/local/jdk-21.0.2
export PATH=$JAVA_HOME/bin:$PATH

Make the script executable:

sudo chmod +x /etc/profile.d/java.sh

Reload the Profile

Apply changes to current session:

source /etc/profile.d/java.sh

For permanent effect, log out and back in.

User-Specific Installation

For single-user installation, add to ~/.bashrc or ~/.zshrc:

# Add to ~/.bashrc
export JAVA_HOME=/usr/local/jdk-21.0.2
export PATH=$JAVA_HOME/bin:$PATH

Apply changes:

source ~/.bashrc

Method 3: Using update-alternatives

For systems with multiple Java versions, use update-alternatives to manage defaults:

# Configure java
sudo update-alternatives --config java

# Configure javac
sudo update-alternatives --config javac

# View all Java-related alternatives
update-alternatives --list java
update-alternatives --list javac

Verifying Installation

Command Line Test

Verify installation:

java -version
javac -version

Expected output for Java 21:

java version "21.0.2" 2024-01-16 LTS
Java(TM) SE Runtime Environment (build 21.0.2+13-LTS-251)
Java HotSpot(TM) 64-Bit Server VM (build 21.0.2+13-LTS-251, mixed mode, sharing)

Check Java Home

Verify JAVA_HOME is set:

echo $JAVA_HOME
# Output: /usr/local/jdk-21.0.2

Running the Java Compiler

which java
which javac

Code Test

Create a test file:

nano hello.java

Add:

public class hello {
    public static void main(String[] args) {
        System.out.println("Hello World from Java " + System.getProperty("java.version") + "!");
    }
}

Compile and run:

javac hello.java
java hello

Output: Hello World from Java 21.0.2!

Managing Multiple JDK Versions

Installing Multiple Versions

Install multiple versions via package manager:

sudo apt install openjdk-21-jdk openjdk-17-jdk openjdk-11-jdk

Switching Between Versions

Using update-alternatives:

sudo update-alternatives --config java

Select the desired version number.

Using JAVA_HOME in scripts:

# Script-specific Java version
export JAVA_HOME=/usr/lib/jvm/java-17-openjdk-amd64
$JAVA_HOME/bin/java -version

Important Notes

CLASSPATH

Note: CLASSPATH is no longer required in modern Java. The JVM and classpath mechanism handle it automatically. For simple compilation and running, Java finds classes in the current directory automatically.

OpenJDK vs Oracle JDK

  • OpenJDK: Free, open source, suitable for most use cases
  • Oracle JDK: Requires license for commercial use, includes additional tools

For production, either works. OpenJDK is recommended for cost-sensitive projects.

Security

  • Always download from official sources
  • Keep JDK updated for security patches
  • Use LTS versions for stability

Troubleshooting

If commands not found:

# Check PATH
echo $PATH

# Verify JAVA_HOME
echo $JAVA_HOME

# Find Java installation
which java
find /usr -name "java" -type f 2>/dev/null

Environment Variables Reference

  • JAVA_HOME: Path to JDK installation
  • PATH: Include $JAVA_HOME/bin for command access
  • CLASSPATH: Usually not needed in modern Java

JDK 17 vs JDK 21

Both are LTS releases. Key differences:

  • JDK 17: Mature, extensive library support, longer track record
  • JDK 21: Latest LTS, virtual threads, pattern matching, better performance

For new projects, JDK 21 offers modern features. For compatibility with existing systems, JDK 17 remains widely supported.

References

Comments