#1
Java is one of the most popular programming languages in the world—and getting it set up on a Linux system is easier than you might think. Whether you're building web apps, desktop software, or learning Java for the first time, setting up your environment properly is the first step.
In this guide, we’ll walk through how to set up the Java environment on Linux, step by step. We’ll cover installing the Java Development Kit (JDK), setting up environment variables, and verifying your setup. No advanced knowledge needed—just follow along, and you’ll be ready to start writing and running Java code in no time!

What You’ll Need

Before we begin, make sure you have:
  • A Linux-based system (Ubuntu, Debian, Fedora, CentOS, etc.)
  • Terminal access
  • sudo privileges (for installing packages)

Step 1: Check If Java Is Already Installed

Open your terminal and run:
java -version
If Java is already installed, you’ll see output showing the version. Something like:
openjdk version "17.0.8"
If you see a “command not found” error, it means Java isn’t installed yet. Let’s fix that next.

Step 2: Install Java on Linux

There are a few versions of Java you can install. The most common ones are:
  • OpenJDK (open-source and widely used)
  • Oracle JDK (official version from Oracle, used in enterprises)

Option A: Install OpenJDK (Recommended for most users)

On Ubuntu/Debian:

sudo apt update && sudo apt install openjdk-17-jdk

On Fedora:

sudo dnf install java-17-openjdk-devel

On CentOS/RHEL:

sudo yum install java-17-openjdk-devel
This will install both the Java runtime (JRE) and the development tools (JDK) needed to compile Java code.

Option B: Install Oracle JDK (Optional)

If you specifically need Oracle's version:
  1. Go to the official Oracle Java downloads page
  2. Download the .tar.gz or .rpm file for Linux
  3. Follow the instructions provided by Oracle to install manually
For most users, OpenJDK is more than enough.

Step 3: Verify Java Installation

Once installed, confirm everything is working:
java -version
Expected output:
openjdk version "17.0.8" 2023-07-18
OpenJDK Runtime Environment ...
Also check if the compiler is available:
javac -version
You should see something like:
javac 17.0.8
If both commands show a version number, you're all set!

Step 4: Set JAVA_HOME Environment Variable

Setting the JAVA_HOME variable helps other programs (like Maven, Gradle, or IDEs) know where Java is installed.

1. Find the path to Java

Run:
readlink -f $(which java)
You might see something like:
/usr/lib/jvm/java-17-openjdk-amd64/bin/java
Your JAVA_HOME is one level up from bin, so in this case:
/usr/lib/jvm/java-17-openjdk-amd64

2. Set JAVA_HOME

Open your terminal and edit your profile file:
For Ubuntu/Debian (bash shell):
nano ~/.bashrc
Or for Zsh users:
nano ~/.zshrc
Add this line at the end of the file:
export JAVA_HOME="/usr/lib/jvm/java-17-openjdk-amd64"
export PATH="$JAVA_HOME/bin:$PATH"
Save the file, then apply the changes:
source ~/.bashrc

Now check:

echo $JAVA_HOME
It should print the path you just set.

Step 5: Compile and Run Your First Java Program

Let’s make sure everything works from start to finish. Create a file called HelloWorld.java:
nano HelloWorld.java
Paste this in:
public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello, Java on Linux!");
    }
}
Save and close the file, then compile it:
javac HelloWorld.java
Now run it:
java HelloWorld
You should see:
Hello, Java on Linux!
Nice work!

image quote pre code