Home Ubuntu How to Set JAVA_HOME Variable Path on Ubuntu

How to Set JAVA_HOME Variable Path on Ubuntu

Basically, the JAVA_HOME Environment path points to the installation location of JDK (Java Development Kit) on your Ubuntu system.

As you develop or run Java-based applications, the associated applications sometimes need to know/reference the JAVA_HOME Environment path to execute/compile without any issues.

Before addressing this Java environment path issue, we need to revise the steps that might/have led us to this article’s objective. We need to start with understanding Java, and its installation, and finally addressing the JAVA_HOME path issue.

Understanding Java

Java has engraved its name in the programming languages’ hall of fame due to its contribution to reputable application footprints and they include:

  • Creation of enterprise software.
  • Scientific computation.
  • Programming of hardware devices.
  • Development of several modern Android applications.
  • Development of mobile java applications.
  • Developer and Big data analytics.
  • Server-side technologies like Apache, GlassFish, and JBoss draw inspiration from Java.

Java Programming Language Superiority

Java is associated with several attributes that have contributed to the bold milestones that have made it successfully achieve the above-mentioned strides. The following factors have contributed to the superiority of Java as a programming language:

  • It keeps evolving despite being mature.
  • It is platform-independent.
  • It simplifies real-time software development.
  • Facilitates embedded computing.
  • Supports IoT (Internet of Things).
  • Simplifies and improves custom Big data solutions.
  • Curtails project overheads.
  • Enriched with library frameworks, IDEs, and development tools.
  • Equipped with a rich set of APIs.
  • It is recommended by Google for Android App Growth.

Java’s Main Components

Java is made up of three main components:

  1. Java Virtual Machine (JVM) – It provides a runtime environment for bytecode execution. It entails specification (JVM’s implementations document), implementation (JVM specification’s requirements program), and runtime instance (needed when writing and running the class-associated commands on the system terminal).
  2. Java Runtime Environment (JRE) – Meets the needed software/program execution requirements like supportive files and class libraries before the associated bytecodes are executed.
  3. Java Development Kit (JDK) – The tools that make up the JDK are Compile, Document, and Package Java Programs. In summary, JDK is responsible for compiling, documenting, and packaging a Java program.

Java Program Execution

The final milestone of understanding Java is comprehending its program execution steps.

Three steps are involved in Java program execution:

  1. Using an editor or IDE to write Java code. The final source code is saved in a file with an .java extension e.g sample_file.java.
  2. Java code compilation via a Java Compiler. Write code is translated from source code to machine code e.g javac sample_file.java.
  3. Execute/run the compiled machine code via Java Runtime e.g java sample_file.

Installing Java and Javac in Ubuntu

A Java Runtime Environment (JRE) like OpenJDK and Java compiler (javac) is needed for you to be able to run Java-based programs.

$ sudo apt install openjdk-17-jre-headless -y
$ sudo apt install openjdk-17-jdk-headless
$ java --version 
$ javac --version 
$ which javac
Install Java in Ubuntu
Install Java in Ubuntu

Set JAVA_HOME Path on Ubuntu

Locate Java’s installation directory:

$ ls -l /usr/lib/jvm
Check Java Install Directory in Ubuntu
Check Java Install Directory in Ubuntu

Temporarily Set JAVA_HOME in Ubuntu

To temporarily set the JAVA_HOME path in Ubuntu.

$ export JAVA_HOME="/usr/lib/jvm/java-17-openjdk-amd64"
$ echo $JAVA_HOME
Temporarily Set Java Path in Ubuntu
Temporarily Set Java Path in Ubuntu

Permanently Set JAVA_HOME in Ubuntu

To permanently set the JAVA_HOME path, add the above line in the /etc/environment file.

$ sudo nano /etc/environment

Add the following line.

JAVA_HOME="/usr/lib/jvm/java-17-openjdk-amd64"

Load the variables.

$ source /etc/environment

To permanently set the JAVA_HOME path to ~/.bashrc file, which gets executed every time a user starts a new terminal session.

$ nano ~/.bashrc

Add the highlighted line at the end of the file:

export JAVA_HOME=/usr/lib/jvm/java-17-openjdk-amd64

Close the file and activate the changes:

$ source ~/.bashrc

Globally Set JAVA_HOME in Ubuntu

To globally set the JAVA_HOME path for all Ubuntu users, edit /etc/bashrc file:

$ sudo nano /etc/bashrc

Add the highlighted line.

export JAVA_HOME=/usr/lib/jvm/java-17-openjdk-amd64

Close the file and activate the changes:

$ source /etc/bashrc

Confirm that the JAVA_HOME variable is still set:

$ echo $JAVA_HOME
Check Java Home Path in Ubuntu
Check Java Home Path in Ubuntu

We have understood how Java works and how easy it is to set the JAVA_HOME path on Ubuntu.

Ravi Saive
I am an Experienced GNU/Linux expert and a full-stack software developer with over a decade in the field of Linux and Open Source technologies. Founder of TecMint.com, LinuxShellTips.com, and Fossmint.com. Over 150+ million people visited my websites.

Each tutorial at UbuntuMint is created by a team of experienced writers so that it meets our high-quality standards.

Was this article helpful? Please add a comment to show your appreciation and support.

1 thought on “How to Set JAVA_HOME Variable Path on Ubuntu”

  1. If you want to avoid manually updating the configuration every time you change Java versions, you can let the operating system handle it for you.

    export JAVA_HOME=$(dirname $(dirname $(readlink -f  /usr/bin/javac)))
    export PATH=$PATH:$JAVA_HOME
    
    Reply

Got something to say? Join the discussion.

Thanks for choosing to leave a comment. Please keep in mind that all comments are moderated according to our comment policy, and your email address will NOT be published or shared. Please Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.