Set Java PATH Variable on Ubuntu 22.04 Permanently

In this guide, you will learn to Set Java PATH Variable on Ubuntu 22.04 Permanently. The Java PATH variable is an environment variable that tells the Ubuntu system where to look for Java executable files when you run Java-related commands from the terminal.

When you type a Java command, such as java or javac, the Ubuntu system searches through directories listed in the PATH variable to find the corresponding executable file. By setting the Java PATH variable, you ensure you can run Java commands from any directory in the terminal without specifying the full path to the Java executable.

Now you can follow the rest of the article to see how to set your Java PATH variable on Ubuntu 22.04 permanently.

Quickly Set Java PATH Variable on Ubuntu 22.04 Permanently

To complete this guide, you must log in to your server as a non-root user with sudo privileges. To do this, you can check the Initial Server Setup with Ubuntu 22.04.

Also, we assumed that you have installed Java on your server.

Step 1 – Locate Java Installation Directory on Ubuntu 22.04

First, you need to find your Java installation path on your server. Typically, it is located under /usr/lib/jvm/ directory. Or you can use the following command to locate it:

sudo update-alternatives --config java

In my case, I get the following output:

Example Output
/usr/lib/jvm/java-11-openjdk-amd64/bin/java

Step 2 – Permanently Set up Java PATH Variable on Ubuntu 22.04

At this point, you can simply add the Java installation directory to your bashrc file. This will set your path variable permanently on your server. To do this, open the file with your desired text editor like Vi Editor or Nano Editor:

sudo vi ~/.bashrc

At the end of the file, add the following line with your Java installation directory:

export PATH="/path/to/java/bin:$PATH"

In my case, it looks like this:

export PATH="/usr/lib/jvm/java-11-openjdk-amd64/bin/java:$PATH"

When you are done, save and close the file.

Then, apply the changes with the command below:

sudo source ~/.bashrc

You can verify that the Java PATH variable is set correctly by running the command below:

echo $PATH
Example Output
/usr/lib/jvm/java-11-openjdk-amd64/bin/java:/usr/local/sbin:/usr/local/bin:/usbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin

That’s it, you are done.

Conclusion

Permanently setting up Java PATH Variable makes using Java-related commands much more convenient. Once the PATH variable is set, you can execute Java commands from any directory without having to specify the full path to the Java executable.

Also, some applications rely on the Java PATH variable being set correctly. By configuring it permanently, you ensure compatibility with these applications and avoid potential issues.

Hope you enjoy it. Also, you may like to read the following articles:

Install Default Java with APT on Ubuntu 22.04

Install Oracle JDK 21 on Ubuntu / Debian

Set and Unset Environment Variables in Linux with Examples

Fix Java Error – Failed To Validate Certificate For IPMI or KVM

Newsletter Updates

Enter your email address below and subscribe to our newsletter

Stay informed and not overwhelmed, subscribe now!