Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set JAVA_HOME in Linux for all users

People also ask

Do I need to set JAVA_HOME on Linux?

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


  1. find /usr/lib/jvm/java-1.x.x-openjdk
  2. vim /etc/profile

    Prepend sudo if logged in as not-privileged user, ie. sudo vim

  3. Press 'i' to get in insert mode
  4. add:

    export JAVA_HOME="path that you found"
    
    export PATH=$JAVA_HOME/bin:$PATH
    
  5. logout and login again, reboot, or use source /etc/profile to 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.


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/

  1. Open terminal and type sudo gedit .bashrc

  2. 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
    
  3. Then save the file and exit from file

Above is for a single user. For all users, you have to follow below steps

  1. gedit /etc/profile

  2. export JAVA_HOME="/usr/lib/jvm/java-8-openjdk-amd64/"

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