Find JAVA_HOME and set it on RHEL

Issue

I had installed java a while ago on my RHEL machine. Now, I’m trying to run a program that requires the JAVA_HOME variable to be set. What is the best way to figure out the installation directory of my java installation and then set JAVA_HOME? Here are the results of running java- version :

java version "1.7.0_25"
Java(TM) SE Runtime Environment (build 1.7.0_25-b15)
Java HotSpot(TM) 64-Bit Server VM (build 23.25-b01, mixed mode)

I have a /usr/lib/jvm directory, but it is empty.

Solution

First, try echo $JAVA_HOME from the command line. Since java is on your path already, JAVA_HOME may be set.

What is the best way to figure out the installation directory of my java installation

Running the command which java will point you to where java is installed.

and then set JAVA_HOME

You can edit ~/.bashrc, ~/.bash_profile, or /etc/profile to set JAVA_HOME. Setting it in ~/etc/profile will set it system wide, and this is probably not what you want. Say for the sake of example the output of which java is /opt/jdk_1.7.0_25, then you’d just add export JAVA_HOME=/opt/jdk_1.7.0_25 to ~/.bashrc or ~/.bash_profile and then run source ~/.bashrc (or source ~/.bash_profile if you set it there).

Note that in this case, java is on the PATH but in some cases you’d need to add export PATH=$PATH:$JAVA_HOME/bin to add the JAVA_HOME variable to the PATH.

Answered By – rpmartz

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published