Java Binary Installation on Ubuntu

Installing the JDK from a binary archive (the official tar.gz) instead of apt gives you a specific Java version in a known location — useful when a project needs Java 21 while your system default is Java 17. This guide covers the full sequence: download and extract, set JAVA_HOME and PATH persistently, verify java and javac, then the same approach for Maven. I also explain why the classic export commands only work in one terminal window — the mistake that makes installations "disappear" after a reboot.

Java Binary Installation on Ubuntu

The JDK is distributed as a tarball containing the bin directory with the java and javac executables. Installing it is two things: put those files somewhere permanent, and tell the shell where they are.

Step 1: Download and Extract the JDK

  1. Download the JDK tarball (for example jdk-21.0.5_linux-x64_bin.tar.gz) from the official distribution — OpenJDK or Oracle JDK, whichever your project requires.
  2. Extract it to a permanent location, e.g. /opt/:
    sudo tar -xzf jdk-21.0.5_linux-x64_bin.tar.gz -C /opt/
  3. Note the extracted directory — that is your JAVA_HOME:
    ls /opt/jdk-21.0.5/bin/java

Step 2: Set JAVA_HOME and PATH (the temporary way)

These commands make Java work in the current terminal only. Useful for a quick test, but gone the moment you close the window:

export JAVA_HOME=/opt/jdk-21.0.5
export PATH=$JAVA_HOME/bin:$PATH

java -version

Step 3: Set JAVA_HOME and PATH (the permanent way)

To keep Java available in every new terminal, append the exports to your shell startup file — ~/.bashrc for Bash, ~/.zshrc for Zsh. Then reload it:

echo 'export JAVA_HOME=/opt/jdk-21.0.5' >> ~/.bashrc
echo 'export PATH=$JAVA_HOME/bin:$PATH' >> ~/.bashrc
source ~/.bashrc

Step 4: Verify the Installation

java -version
javac -version
echo $JAVA_HOME

Expected output for JDK 21:

openjdk version "21.0.5" 2024-10-15
OpenJDK Runtime Environment ...
javac 21.0.5

Note: java -version alone only proves a runtime exists; javac -version proves the compiler is present too, which you need for building code.

Managing Multiple Java Versions

If several JDKs are installed, the last one added to PATH wins. To see what is active:

update-alternatives --config java
update-alternatives --config javac

These commands (from the java-common package) let you switch the system-wide default. For a project-specific version, override JAVA_HOME per terminal instead:

export JAVA_HOME=/opt/jdk-17

Installing Maven Binary on Ubuntu

Maven follows the identical pattern: extract, point a variable at it, add its bin to the PATH.

M2_HOME=/opt/mvn-3-2-3/
PATH=$M2_HOME/bin:$PATH
export PATH

mvn -version

For a permanent install, add the same lines to ~/.bashrc and run source ~/.bashrc.

Bonus: Getting Started with Ansible

Once Java and Maven are on the PATH, you often need to manage the machines you deploy to. Ansible does this over SSH with no agent required. Here is the minimal setup I use.

1. Install Ansible

sudo apt install ansible

2. Create ansible.cfg

Ansible reads this config from the current directory before any run:

[defaults]
inventory = ./inventory.ini
host_key_checking = False
retry_files_enabled = False

3. Create inventory.ini

Group servers and tell Ansible which user and SSH key to use for each:


[enum]
172.16.3.23 ansible_user=cloud-user ansible_ssh_private_key_file=/home/rinas/.ssh/m1-sec.pem

[radius]
172.27.2.43 ansible_user=cloud-user ansible_ssh_private_key_file=/home/rinas/.ssh/m1-radius.pem

[hutch]
172.16.3.114 ansible_user=centos ansible_ssh_private_key_file=~/.ssh/ansible-keypair.pem

4. Test Connectivity Without a Playbook

ansible all -m ping -i inventory.ini

Every reachable host answers pong.

5. Run a Playbook

Create a playbook file (e.g. my-playbook.yml) and execute it against the inventory:

---
- name: Gather system information
  hosts: all
  gather_facts: yes
  tasks:
    - name: Display disk usage
      ansible.builtin.command: df -h
      register: disk_usage

    - name: Show disk usage output
      ansible.builtin.debug:
        var: disk_usage.stdout_lines

    - name: Check uptime
      ansible.builtin.command: uptime
      register: system_uptime

    - name: Show system uptime
      ansible.builtin.debug:
        var: system_uptime.stdout
ansible-playbook my-playbook.yml -i inventory.ini

For more on the SSH keys used here, see the SSH setup guide.

Common Problems

Problem 1: java works, but "javac: command not found"

Cause: you installed a JRE (runtime only), not a JDK (includes the compiler).

Solution: download the JDK tarball, not the JRE, and re-verify with javac -version.

Problem 2: java works in one terminal but not in a new one

Cause: you used export at the prompt — it only affects the current session.

Solution: add the export lines to ~/.bashrc and open a new terminal.

Problem 3: the wrong Java version is active

Cause: several JDKs are on the PATH and another one comes first, or a distribution default took over.

Solution: check which java and echo $JAVA_HOME, then fix the order in ~/.bashrc or use update-alternatives --config java.

Problem 4: "Error: A JNI error has occurred" when running SDK tools

Cause: the tool was compiled for a newer Java than the active runtime.

Solution: point JAVA_HOME at a newer JDK (e.g. JDK 17+ for Android tools).

Best Practices

Conclusion

We installed the JDK from a binary archive, made JAVA_HOME and PATH persistent in ~/.bashrc, verified both java and javac, applied the same pattern to Maven, and set up a minimal Ansible inventory for server management. The key idea: a binary install is just a folder plus a PATH entry — persistence is the detail that makes it survive a reboot.

Related Articles

A.M. Rinas

Written by A.M. Rinas

Full-Stack Software Engineer working on web applications, DevOps automation, and databases. More about me on the about page, or say hi through the contact page and LinkedIn.

← Back to all articles