43

I have install java through yum on CentOS, however another java programme needs to know what the JAVA_HOME environmental variable is. I know all about setting environmental variables, but what do I set it to? java is installed in /usr/bin/java, it can't be there!

Amandasaurus
  • 30,211
  • 62
  • 184
  • 246

8 Answers8

59

Actually I found it,

it's /usr/lib/jvm/jre-1.6.0-openjdk.x86_64/. I found out what it was by doing update-alternatives --display java and it showed me the directory /usr/lib/jvm/jre-1.6.0-openjdk.x86_64/bin/java

Amandasaurus
  • 30,211
  • 62
  • 184
  • 246
20

I'm not quite sure but if you install the normal RPMS the JAVA_HOME value can also be set to this:

/usr/java/default/

EDIT: I just checked on my home system. I have created this file:

/etc/profile.d/java.sh

That contains:

export JAVA_HOME=/usr/java/default/

and I'm using the official version from Sun: jdk-1.6.0_12-fcs

EDIT: Here is how I set up Java on my machine:

Install Java

Download and install Java JDK from Oracle

Make it primary

Ensure this Java is used instead of the OpenJDK version using the following two commands:

First

alternatives --install /usr/bin/java java /usr/java/default/bin/java 999999 \
           --slave /usr/bin/keytool keytool /usr/java/default/bin/keytool \
           --slave /usr/bin/rmiregistry rmiregistry /usr/java/default/bin/rmiregistry

Second

alternatives --install /usr/bin/javac javac /usr/java/default/bin/javac 999999 \
           --slave /usr/bin/jar jar /usr/java/default/bin/jar \
           --slave /usr/bin/rmic rmic /usr/java/default/bin/rmic

Set JAVA_HOME

Ensure all users have their JAVA_HOME environment variable set to the correct value:

echo "export JAVA_HOME=/usr/java/default/" > /etc/profile.d/java_home.sh
Andrew
  • 103
  • 3
Niels Basjes
  • 2,176
  • 3
  • 18
  • 26
3

You want to set JAVA_HOME to be the root location of the JDK or JRE, for example:

export JAVA_HOME=/usr/java/jdk1.3

If the JDK (Development kit) is installed, ypu probably want it to point to that, else use the JRE path (Java Runtime Environment). Then, you might want to set your $PATH environment variable to include the bin directory as well:

export PATH=$PATH:/usr/java/jdk1.3/bin

If you are using tomcat, you might also need to set CATALINA_HOME for the tomcat user.

export CATALINA_HOME=/path/to/tomcat

To set this for the system you want to edit your /etc/profile or add a .sh file in /etc/profile.d folder. For a particular user you can put it in the users ~/.profile or ~/.bash_profile files.

Kyle Brandt
  • 82,107
  • 71
  • 302
  • 444
  • I wonder if there is a good reason the install doesn't do this for you if run as the super user, I always find it irritating ... – Kyle Brandt Aug 06 '09 at 13:15
2

Below is always working for me perfectly:

[user@base ~]$ locate bin/java
/usr/bin/java
/usr/bin/javac
/usr/bin/javadoc
/usr/bin/javaws
/usr/java/jdk1.6.0_31/bin/java
/usr/java/jdk1.6.0_31/bin/javac
/usr/java/jdk1.6.0_31/bin/javadoc
/usr/java/jdk1.6.0_31/bin/javah
/usr/java/jdk1.6.0_31/bin/javap
/usr/java/jdk1.6.0_31/bin/javaws
/usr/java/jdk1.6.0_31/jre/bin/java
/usr/java/jdk1.6.0_31/jre/bin/java_vm
/usr/java/jdk1.6.0_31/jre/bin/javaws

It means I can set JAVA_HOME as /usr/java/jdk1.6.0_31

prashant
  • 121
  • 3
1

I considered the problem of if one changes the version of java being used with:

alternatives --config java

Then any hard coded JAVA_HOME is going to change (atleast it will on the Centos 6.6 I'm currently staring at). That is, there is no /usr/java.

This doesn't seem like the best way (open to failure) but in the 10 mins I've spent on this it seems the best.

In /etc/bashrc I modified the setting to JAVA_HOME to be:

export JAVA_HOME=$(alternatives --display java | grep current | sed 's/link currently points to //' | sed 's|/bin/java||')

You have to do something similar on MacOSX but without all the grep and sed to parse out the result. Surely alternatives offers an similarly easier solution.

Anyways I hope I helped.


Update

No that would be JRE_HOME. The JDK / SDK isn't proving as obvious (I'll keep looking).

The alternatives seems to only be about the JRE by default. I defer to @Niels answer or just install Java with yum and set the JAVA_HOME to that (the trick is where I found where that was installed to!).

yum install java-1.8.0-openjdk-devel.x86_64
cd /usr/lib/jvm 

I noticed java_1.8.0 is a symlink to /etc/alternatives/java_sdk_1.8.0 and so set my $JAVA_HOME to /usr/lib/jvm/java_1.8.0. In the /etc/bashrc.

HankCa
  • 141
  • 5
0

Thats weird, the whole point of having a JAVE_HOME environmental variable is so you don't have to specify the path to each program, it can get this path by looking for this variable.

Either way, the correct way to find the environmental variable for JAVA_HOME is to use echo:

[root@host ~]# echo $JAVA_HOME
/usr/java/j2sdk1.4.2_09

Running this command will display the path no matter what version you are running, from command line.

Dave Drager
  • 8,315
  • 28
  • 45
0

re: Centos7, refer to the /etc/java/java.conf file.

in it JAVA_HOME is indicated to be located in the following dir : /usr/lib/jvm/java

i checked, it is the correct version of the jdk I loaded via yum, and so...

my env doesn't have JAVA_HOME set by default, therefore I set it in .bash_profile

theRiley
  • 101
  • 2
0

I found the package manager can place in a variety of locations, so using which (you will need to explicitly install this if using a container) the following is a little less brittle

javaCompiler=$(which javac)
javaBin=$(dirname $(readlink -f $javaCompiler))
javaHome=${javaBin%/*}
export JAVA_HOME=$javaHome

or for JRE

javaExec=$(which java)
javaBin=$(dirname $(readlink -f $javaExec))
javaHome=${javaBin%/*}
export JRE_HOME=$javaHome
Jules Clements
  • 201
  • 2
  • 8