Find JAVA_HOME and set it on RHEL

readlink command will show you full path of symbolic link:

readlink -f `which java`

The best you can do is avoid Red Hat's java altogether.

Get your java from Oracle and put it in /opt. Then just create symlink /opt/java -> /opt/jdk-someversion, and create /etc/profile.d/java.sh containing

#!/bin/sh
export JAVA_HOME=/opt/java
export PATH=$JAVA_HOME/bin:$PATH

Then, to change system-wide java, just change symlink in opt. To use multiple java versions, use scripts like the above with appropriate JAVA_HOME.

Furthermore, /sbin/service script used to run /etc/init.d scripts will rip off environment variables - executes env -i explicitly. So i.e. your tomcat will not get JAVA_HOME, you'll have to create setenv.sh in $CATALINA_BASE/bin.

Drawback to this approach is you don't get java updates from Red Hat.


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.


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".

Tags:

Linux

Java