If you are running Java programs on Ubuntu using Eclipse, Maven or Netbeans etc, you'll need to set JAVA_HOME to your path. Otherwise, your system will complain that “java_home environment variable is not set”.
find /usr/lib/jvm/java-1.x.x-openjdk
vim /etc/profile
Prepend sudo if logged in as not-privileged user, ie. sudo vim
add:
export JAVA_HOME="path that you found"
export PATH=$JAVA_HOME/bin:$PATH
source /etc/profile
to apply changes immediately in your current shellFor 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.
You could use /etc/profile or better a file like /etc/profile.d/jdk_home.sh
export JAVA_HOME=/usr/java/jdk1.7.0_05/
You have to remember that this file is only loaded with new login shells.. So after bash -l or a new gnome-session and that it doesn't change with new Java versions.
None of the other answers were "sticking" for me in RHEL 7, even setting JAVA_HOME
and PATH
directly in /etc/profile
or ~/.bash_profile
would not work. Each time I tried to check if JAVA_HOME
was set, it would come up blank:
$ echo $JAVA_HOME
(<-- no output)
What I had to do was set up a script in /etc/profile.d/jdk_home.sh
:
#!/bin/sh
export JAVA_HOME=/opt/ibm/java-x86_64-60/
export PATH=$JAVA_HOME/bin:$PATH
I initially neglected the first line (the #!/bin/sh
), and it won't work without it.
Now it's working:
$ echo $JAVA_HOME
/opt/ibm/java-x86_64-60/
Open terminal and type sudo gedit .bashrc
It will ask you your password. After typing the password, it will open the bash file. Then go to end and type:
export JAVA_HOME="/usr/lib/jvm/java-8-openjdk-amd64/"
export PATH=$PATH:$JAVA_HOME/bin
Then save the file and exit from file
Above is for a single user. For all users, you have to follow below steps
gedit /etc/profile
export JAVA_HOME="/usr/lib/jvm/java-8-openjdk-amd64/"
export PATH=$PATH:$JAVA_HOME/bin
Copy the bin file path you installed
YOUR PATH
open terminal and edit environment file by typing following command,
sudo nano /etc/environment
In this file, add the following line (replacing YOUR_PATH
by the just copied path):
JAVA_HOME="YOUR_PATH"
That should be enough to set the environment variable. Now reload this file:
source /etc/environment
now test it by executing:
echo $JAVA_HOME
Doing what Oracle does (as a former Sun Employee I can't get used to that one)
ln -s latestJavaRelease /usr/java/default
Where latestJavaRelease is the version that you want to use
then export JAVA_HOME=/usr/java/default
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With