You can set your JAVA_HOME in /etc/profile as Petronilla Escarabajo suggests. But the preferred location for JAVA_HOME or any system variable is /etc/environment.
Open /etc/environment in any text editor like nano or gedit and add the following line:
JAVA_HOME="/usr/lib/jvm/open-jdk"
(java path could be different)
Use source to load the variables, by running this command:
source /etc/environment
Then check the variable, by running this command:
echo $JAVA_HOME
Update
Usually most linux systems source /etc/environment by default. If your system doesn't do that add the following line to ~/.bashrc (Thanks @pje)
source /etc/environment
Answer from Manula Waidyanatha on askubuntu.comYou can set your JAVA_HOME in /etc/profile as Petronilla Escarabajo suggests. But the preferred location for JAVA_HOME or any system variable is /etc/environment.
Open /etc/environment in any text editor like nano or gedit and add the following line:
JAVA_HOME="/usr/lib/jvm/open-jdk"
(java path could be different)
Use source to load the variables, by running this command:
source /etc/environment
Then check the variable, by running this command:
echo $JAVA_HOME
Update
Usually most linux systems source /etc/environment by default. If your system doesn't do that add the following line to ~/.bashrc (Thanks @pje)
source /etc/environment
To set JAVA_HOME environment variable, do the following:
- Launch Terminal by pressing Ctrl+Alt+T on your keyboard.
- Enter the following command:
$gksudo gedit /etc/environment - Depending on where you installed your Java, you will need to provide the full path. For this example, I installed Oracle JDK 7 in the
/usr/lib/jvm/java-7-oracledirectory.
Scroll to the end of the file and enter the following:
JAVA_HOME=/usr/lib/jvm/java-7-oracle
export JAVA_HOME - Save your file and exit gedit.
- Lastly, reload the system PATH with the following command:
$. /etc/environment
The above method will save you the hassle in having to run the commands every time you log in to your computer.
find /usr/lib/jvm/java-1.x.x-openjdkvim /etc/profilePrepend sudo if logged in as not-privileged user, ie.
sudo vim- Press 'i' to get in insert mode
add:
export JAVA_HOME="path that you found" export PATH=$JAVA_HOME/bin:$PATH- logout and login again, reboot, or use
source /etc/profileto apply changes immediately in your current shell
For all users, I would recommend creating a file in /etc/profile.d/java_home.sh the following lines
# Set JDK installation directory according to selected Java compiler
export JAVA_HOME=$(readlink -f /usr/bin/javac | sed "s:/bin/javac::")
This will update dynamically and works well with the alternatives system. Do note though that the update will only take place in a new login shell.
Videos
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
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