Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find JAVA_HOME and set it on RHEL

Tags:

java

linux

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.

like image 932
Rohit Pandey Avatar asked Mar 10 '14 01:03

Rohit Pandey


People also ask

Where is java home in redhat?

First, try echo $JAVA_HOME from the command line. Since java is on your path already, JAVA_HOME may be set. Running the command which java will point you to where java is installed.

Where is JAVA_HOME set?

To set JAVA_HOME, do the following: Right click My Computer and select Properties. On the Advanced tab, select Environment Variables, and then edit JAVA_HOME to point to where the JDK software is located, for example, C:\Program Files\Java\jdk1.


2 Answers

RHEL uses alternatives subsystem to manage java installations. You can have multiple versions of java installed, but only one is active at a time.

This means that running which java doesn't provide useful information. The output would be the same no matter which java installation is selected via alternatives. Running readlink -f $(which java) (as already suggested in other comment) or using asking alternatives alternatives --display java would be better.

See example from RHEL 6 machine with OpenJDK installed (which is shipped with RHEL):

[root@example ~]# which java
/usr/bin/java
[root@example ~]# readlink -f $(which java)
/usr/lib/jvm/java-1.7.0-openjdk-1.7.0.79.x86_64/jre/bin/java
[root@example ~]# alternatives --display java | head -2
java - status is manual.
 link currently points to /usr/lib/jvm/jre-1.7.0-openjdk.x86_64/bin/java

Note that enviroment variable JAVA_HOME is not defined anywhere by default, you would need to define it yourself in .bashrc of user which requires it.

In previous example, correct value of JAVA_HOME would be /usr/lib/jvm/java-1.7.0-openjdk-1.7.0.79.x86_64.

See details in Install OpenJDK documentation, search for section "Optional: Set the JAVA_HOME environment variable".

like image 136
marbu Avatar answered Sep 21 '22 09:09

marbu


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.

like image 21
rpmartz Avatar answered Sep 20 '22 09:09

rpmartz