ProgramGuru

Install On Linux

Install On Linux

A school program needs reliable data and clear rules. Install a JDK using your distribution’s supported package source, then check java and javac in the same terminal. Package names and installation paths vary by distribution.

  1. Understand

    Install a JDK using your distribution’s supported package source, then check java and javac in the same terminal.

  2. Apply

    Trace the Java example, predict its output and explain the result before running it.

  3. Avoid mistakes

    A runtime-only installation may run programs while lacking javac. Do not assume every Linux distribution uses apt.

Install On Linux

In this guide, we’ll walk you through how to install Java on any major Linux distribution.

We will cover

  • How to check if Java is already installed
  • Installing OpenJDK via package manager
  • Installing Oracle JDK manually
  • Setting JAVA_HOME and updating PATH
  • Verifying installation with Java programs

Step 1: Check if Java is Already Installed

Before you install anything, it's good practice to check whether Java is already on your system.

java -version
Command 'java' not found, but can be installed with:
sudo apt install default-jre

If you see something like the above message, Java isn’t currently installed. If Java is installed, you'll see an output like this:

openjdk version "17.0.8" 2023-07-18
OpenJDK Runtime Environment (build 17.0.8+7-Ubuntu-222.04)
OpenJDK 64-Bit Server VM (build 17.0.8+7-Ubuntu-222.04, mixed mode, sharing)

Step 3: Install Oracle JDK (Optional)

If your project specifically requires Oracle JDK, you can download and install it manually. Oracle JDK includes some commercial features and licensing considerations.

Download and Extract Oracle JDK

Go to the official Oracle JDK downloads page and download the appropriate version for Linux. Then run:

cd ~/Downloads
tar -xvf jdk-21_linux-x64_bin.tar.gz
sudo mv jdk-21 /usr/lib/jvm/jdk-21

Configure Oracle JDK as Default

sudo update-alternatives --install /usr/bin/java java /usr/lib/jvm/jdk-21/bin/java 1
sudo update-alternatives --config java

Step 4: Set JAVA_HOME and Update PATH

It's important to let your system know where Java is located. To do this, follow these steps:

1. Open the terminal and edit the shell configuration file

Use any text editor to open your shell configuration file. Use the command based on your shell:

For bash users:

nano ~/.bashrc

For zsh users:

nano ~/.zshrc

2. Add the following lines at the end of the file:

export JAVA_HOME=/usr/lib/jvm/jdk-21
export PATH=$JAVA_HOME/bin:$PATH

3. Save and close the file

In nano, press Ctrl + O to save, then Ctrl + X to exit.

4. Apply the changes

Run the following command to apply the changes immediately:

For bash users:

source ~/.bashrc

For zsh users:

source ~/.zshrc

Step 5: Test Java Installation

Let’s test our installation with a simple Hello World program:

public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello, Java on Linux!");
    }
}

Compile:

javac HelloWorld.java

Run:

java HelloWorld
Hello, Java on Linux!

If you see this output, your Java installation is working perfectly!

Final Thoughts

For most developers, OpenJDK is sufficient and easily manageable through package managers. However, if you’re working in an enterprise setting where Oracle JDK is mandatory, manual installation offers full control.

Now that Java is ready to go, you can begin building Java applications.

Important boundaries and mistakes

A runtime-only installation may run programs while lacking javac. Do not assume every Linux distribution uses apt.

Try the school example

Before running this Java program, write down each line you expect to see. Identify the value, branch, object or exception responsible for each line. Then explain whether the following assumption is valid: A runtime-only installation may run programs while lacking javac.

Java 17 · Main.java · complete practice program

public class Main  {
  public static void main(String[] args)  {
    System.out.println(System.getProperty("java.specification.version"));
  }
}

Worked answer and reasoning

Java 17 · Main.java · source repeated for this answer

public class Main  {
  public static void main(String[] args)  {
    System.out.println(System.getProperty("java.specification.version"));
  }
}

Expected output on the Java 17 teaching runtime; your installed version can differ

17

Install a JDK using your distribution’s supported package source, then check java and javac in the same terminal. Package names and installation paths vary by distribution.

A runtime-only installation may run programs while lacking javac. Do not assume every Linux distribution uses apt.

Run with javac Main.java, then java Main. The declaration of Main names the class; main starts the example, and println writes a complete output line.

Check your understanding

For the complete practice program in this lesson, which output is expected on Java 17?
Choose one answer. You can try again.

Answer and explanation

17

Install a JDK using your distribution’s supported package source, then check java and javac in the same terminal. Package names and installation paths vary by distribution. A runtime-only installation may run programs while lacking javac. Do not assume every Linux distribution uses apt.

What to remember

Install a JDK using your distribution’s supported package source, then check java and javac in the same terminal. Package names and installation paths vary by distribution. A runtime-only installation may run programs while lacking javac. Do not assume every Linux distribution uses apt.

Related explanations: Java Variables, Java If Statement - Syntax and Examples, Java Class - Blueprint of Objects, Exceptions in Java Introduction.

Supporting documentation: Official Java learning guide and Java 17 language specification.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: Install On Windows 10 →