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
/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.
[ac-button size=”large” color=”green” style=”flat” icon=”fa-link” url=”https://www.2daygeek.com/install-java-openjdk-6-7-8-on-ubuntu-centos-debian-fedora-mint-rhel-opensuse-manjaro-archlinux/” target=”_blank”]How to Install Java in Linux[/ac-button]
[ac-button size=”large” color=”orange” style=”flat” icon=”fa-link” url=”https://www.2daygeek.com/category/java/” target=”_blank”]Check Available Java articles for Linux[/ac-button]
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
.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
.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
/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.
17 Comments on “How to setup JAVA Environment Variables on Linux”
Great article! Thank you very much!
Great article! Thank you very much!
I have my $JAVA_HOME, $JAVAC_HOME and $JRE_HOME located in
/usr/lib/jvm… Will that cause any issues? So far things have been smooth
with every Java application I have run. But I noticed that in the above section
regarding “3) Permanent : Setup JAVA Environment Variable to single user”
your paths to Java are in /opt
Just a thought – Please let me know if you think I should change my setup
i want cancle this work . can i reset this configure? please give me a mail. my mail address is [ [email protected] ]
These are the easiest and most comprehensive directions to follow! I will come to 2daygeek for all of my Linux questions from now on. Thank you.
Al igual que en los últimos años, varias tiendas mexicanas del DF,
Puebla, Guadalajara y Monterrey se han sumado a este esfuerzo.
OMG! This was a straight forward tutorial to a Linux newbie, thank you geek you made me understand everything about setting Java environment in less than 5 minutes! Great job buddy. 🙂
Nice to hear, you have setup JAVA environment without any trouble. Thanks for your prompt replay.
This cleared up a lot of confusion for me. This is the second or third time re-configuring the JDK. I was able to follow the symbolic links to find out where the real java was hidden. Changed the “export and PATH’ for the CENTOS 7 environment.
all works. but decided to use .bash_profile instead of .bashrc
i have to version of java . one was preloaded with kali disto , one i want to install is jdk1.8 . i follow above steps to set path for 1.8 but when i am executing java-version command it shows 1.6 not 1.8
Did you installed Java 1.8 ? If so, have you installed using alternative method or anyother. If you have both java, surely you can set the Environment Variable.
The settings for JAVA_HOME are incorrect in your example and won’t work.
should be something like:
Still i’m using the same environmental variable on my openfire chat server and working fine. Will cross check your update.
Many thanks for the clear instructions.
Used to set the software framework for Android Studio IDE.
Nice to hear.