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:
- 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).
- Java Runtime Environment (JRE) – Meets the needed software/program execution requirements like supportive files and class libraries before the associated bytecodes are executed.
- 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:
- 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. - Java code compilation via a Java Compiler. Write code is translated from source code to machine code e.g javac sample_file.java.
- 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
Set JAVA_HOME Path on Ubuntu
Locate Java’s installation directory:
$ ls -l /usr/lib/jvm
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
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
We have understood how Java works and how easy it is to set the JAVA_HOME path on Ubuntu.
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.