Linux JDK Installation

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.

1. Download JDK from Oracle Website

Visit Oracle JDK Downloads and download the appropriate version for your platform and system. For example: jdk-8u301-linux-x64.tar.gz (version numbers vary).

2. Extract JDK to /usr/local/ Directory

sudo tar -zxvf jdk*.tar.gz -C /usr/local/

This extracts the JDK to /usr/local/jdk1.8.0_301 (adjust version).

3. Configure Environment Variables

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

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

Add the following:

export JAVA_HOME=/usr/local/jdk1.8.0_301
export PATH=$JAVA_HOME/bin:$PATH
  • Note: CLASSPATH is no longer required in modern Java; the JVM handles it automatically.

4. Make Variables Effective

Reload the profile:

source /etc/profile.d/java.sh

For immediate effect in the current session, or log out and back in.

5. Command Line Test

Verify installation:

java -version
javac -version

You should see the JDK version.

6. Code Test

Create a test file:

nano hello.java

Add:

public class hello {
    public static void main(String[] args) {
        System.out.println("Hello World!");
    }
}

Compile and run:

javac hello.java
java hello

Output: Hello World!

Important Notes

  • Alternatives: Use OpenJDK for free: sudo apt install openjdk-11-jdk (Ubuntu) or sudo yum install java-11-openjdk (CentOS).
  • Multiple JDKs: Use update-alternatives to manage: sudo update-alternatives --config java.
  • Security: Always download from official sources and keep JDK updated.
  • User-Specific: For single user, add exports to ~/.bashrc instead of /etc/profile.d/.
  • Troubleshooting: If commands not found, check PATH with echo $PATH.

References