Reputation: 2681
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.
Upvotes: 25
Views: 97302
Reputation: 21
For me worked to change link in
/etc/alternatives for:
java -> /app/java/jdk8u222-b10/jre/bin/java
I know this is old java, but this server was neglected in our company.
Upvotes: 2
Reputation: 170
At least on RHEL 7, alternatives
sets up a slave link for java_sdk
at:
/etc/alternatives/java_sdk/
This is a symlink to the root of the SDK installation.
Upvotes: 3
Reputation: 101
I found a way to combine the above so that I can programmaticly set JAVA_HOME
.
export JAVA_HOME=$(dirname $(readlink -f $(which java))|sed 's^jre/bin^^')
On Ubuntu, returns /usr/lib/jvm/java-8-openjdk-amd64/. On CentOS7, returns /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.201.b09-2.el7_6.x86_64/.
This can be added to a Makefile with
JAVA_HOME = $(shell dirname $$(readlink -f $$(which java))|sed 's^jre/bin^^')
This is much safer than exporting a hardcoded path and is more portable.
Upvotes: 3
Reputation: 352
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.
Upvotes: 6
Reputation: 2021
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".
Upvotes: 36
Reputation: 1462
readlink
command will show you full path of symbolic link:
readlink -f `which java`
Upvotes: 12
Reputation: 3809
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
.
Upvotes: 11