How to setup JAVA Environment Variables on Linux

Most of us still getting confusion to Setup JAVA Environment Variable in Linux. If you are using Linux box JAVA is necessary to run certain applications which is based on Java.

Java installation is not a problem for everybody but Settingup JAVA Environment Variable is problem.

If you did anything wrong on .bashrc or /etc/profile file none of the bash command will work. I faced the same problem when i setup JAVA Environment Variable in first time.

After that i have installed java many times and did some research and finally came to know Settingup JAVA Environment Variable in Linux is not a big deal. Follow the below steps to Setup JAVA Environment Variable in Linux Box without any trouble.

First we have to install Java. Once Java installed successfully, then follow the below steps for further setup.

How to Install Java in Linux

Check Available Java articles for Linux

Locate the java installation directory

Use the following command to locate the java installation directory so that you can setup the JAVA Environment Variable. It shows java installation location is below.

$ which java
/usr/bin/java
or
$ whereis java
java: /usr/bin/java /usr/bin/X11/java /usr/share/java

When I’m checking the below file, it is linked with another file as symbolic link. So it is not a correct path and we need a exact path.

$ ls -lah /usr/bin/java
lrwxrwxrwx 1 root root 22 Aug 12 16:46 /usr/bin/java -> /etc/alternatives/java

I’m navigating to above symbolic link. Now, it is showing absolute path. So the original java installation path is /opt/jdk1.8.0_20/bin/java.

$ ls -lah /etc/alternatives/java
lrwxrwxrwx 1 root root 25 Aug 22 16:44 /etc/alternatives/java -> /opt/jdk1.8.0_20/bin/java

Temporary : Setup JAVA Environment Variable to all user’s

Use the below commands to Setup Temporary JAVA Environment Variables because java based application’s uses environment variables. In Linux setup environment variables is very easy. You just export JAVA_HOME, JAR_HOME & PATH that’s it.

$ export JAVA_HOME=/opt/jdk1.8.0_20/bin/java

$ export JRE_HOME=/opt/jdk1.8.0_20/bin/jre/bin/java

$ export PATH=$PATH:$HOME/bin:JAVA_HOME:JRE_HOME

$ echo $JAVA_HOME
/opt/jdk1.8.0_20/bin/java

$ echo $JRE_HOME
/opt/jdk1.8.0_20/bin/jre/bin/java

Permanent : Setup JAVA Environment Variable to single user

Open .bashrc file on your favorite text editor and add the below colored line to end of the file. Make sure you need to mention your path instead of us. Save and exit.

$ nano .bashrc


export JAVA_HOME=/opt/jdk1.8.0_20/bin/java
export JRE_HOME=/opt/jdk1.8.0_20/jre/bin/java
export PATH=$PATH:$HOME/bin:JAVA_HOME:JRE_HOME

After saving .bashrc file, run the following command to make it work

$ source ~/.bashrc

Now, check the environment value using below commands. Its clearly fetch the path of jdk and jar home based on our new setup.

$ echo $JAVA_HOME
/opt/jdk1.8.0_20/bin/java

$ echo $JRE_HOME
/opt/jdk1.8.0_20/jre/bin/java

Permanent : Setup JAVA Environment Variable to all user’s

Open /etc/profile file on your favorite text editor and add the below colored line to end of the file. Make sure you need to mention your path instead of us. Save and exit.

$ nano /etc/profile


JAVA_HOME="/usr/lib/jvm/java-7-openjdk-amd64/bin/java"
JRE_HOME="/usr/lib/jvm/java-7-openjdk-amd64/jre/bin/java"
PATH=$PATH:$HOME/bin:JAVA_HOME:JRE_HOME

After saving profile file, run the below command to make it work

$ . /etc/profile

Now, check the environment value using below commands. Its clearly fetch the path of jdk and jre home based on our new setup.

$ echo $JAVA_HOME
/usr/lib/jvm/java-7-openjdk-amd64/bin/java

$ echo $JRE_HOME
/usr/lib/jvm/java-7-openjdk-amd64/jre/bin/java

We are preparing all articles in-depth to understand by all level/stage Linux administrators. If the article is useful for you, then please spend less than a minute to share your valuable comments in our commenting section.

Please stay tune with us…Good Luck.

Magesh Maruthamuthu

Love to play with all Linux distribution

You may also like...