[java] How to set JAVA_HOME in Linux for all users

I am new to Linux system and there seem to be too many Java folders.

java -version gives me:

  • java version "1.7.0_55"
  • OpenJDK Runtime Environment (rhel-2.4.7.1.el6_5-x86_64 u55-b13)
  • OpenJDK 64-Bit Server VM (build 24.51-b03, mixed mode)

When I am trying to build a Maven project , I am getting error:

Error: JAVA_HOME is not defined correctly.
We cannot execute /usr/java/jdk1.7.0_05/bin/java

Could you please tell me which files I need to modify for root as well as not-root user and where exactly is java located?

This question is related to java linux java-home path-variables

The answer is


For all users, I would recommend placing the following line in /etc/profile

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.


In /etc/profile , if you open that will you’ll get to know that IT IS no recommended to write on that file. Instead of that make a script of your commands(suppose test.sh)go to /etc/profile.d folder and Put test.sh there. Every time you instance reboot it’ll be automatically called by /etc/profile.


I use the line:

export JAVA_HOME=$(readlink -f $(dirname $(readlink -f $(which java) ))/../)

to my ~/.profile so it uses the base of the default java directory at login time. This is for bash.


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

It's Very easy to set a path in Linux. Do as follows :

Step-1 Open terminal and type sudo gedit .bashrc

Step-2 It will ask you your password. After typing the password ,it will open the bash file. Then go to end and type below

step-3

   export JAVA_HOME="/usr/lib/jvm/java-8-openjdk-amd64/"
   export PATH=$PATH:$JAVA_HOME/bin

step-4 Then save the file and exit from file

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

Step-1 gedit /etc/profile

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

Step-3 export PATH=$PATH:$JAVA_HOME/bin

Hope this helps. Thanks!


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.


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


This is a very simple script to solve the problem

export JAVA_HOME_BIN=`which java`
export JAVA_HOME_DIR=`dirname $JAVA_HOME_BIN`
export JAVA_HOME=`dirname $JAVA_HOME_DIR`

And for testing:

echo $JAVA_HOME

1...Using the short cut Ctlr + Alt + T to open terminal

2...Execute the below command:

echo export JAVA_HOME='$(readlink -f /usr/bin/javac | sed "s:/bin/javac::")' | sudo tee /etc/profile.d/jdk_home.sh > /dev/null

3...(Recommended) Restart your VM / computer. You can use source /etc/source if don't want to restart computer

4...Using the short cut Ctlr + Alt + T to open terminal

5...Verified JAVA_HOME installment with

echo $JAVA_HOME

One-liner copy from flob, credit to them


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/

All operational steps(finding java, parent dir, editing file,...) one solution

zFileProfile="/etc/profile"
zJavaHomePath=$(readlink -ze $(which java) | xargs -0 dirname | xargs -0 dirname)
echo $zJavaHomePath

echo "export JAVA_HOME=\"${zJavaHomePath}\"" >> $zFileProfile
echo "export PATH=\$PATH:\$JAVA_HOME/bin" >> $zFileProfile

Result:

# tail -2 $zFileProfile
export JAVA_HOME="/usr/lib/jvm/java-11-openjdk-11.0.7.10-1.el8_1.x86_64"
export PATH=$PATH:$JAVA_HOME/bin

Explanation:

1) Let's break the full command into pieces

$(readlink -ze $(which java) | xargs -0 dirname | xargs -0 dirname)

2) Find java path from java command

# $(which java)
"/usr/bin/java"

3) Get relative path from symbolic path

# readlink -ze /usr/bin/java
"/usr/lib/jvm/java-11-openjdk-11.0.7.10-1.el8_1.x86_64/bin/java"

4) Get parent path of /usr/lib/jvm/java-11-openjdk-11.0.7.10-1.el8_1.x86_64/bin/java

# readlink -ze /usr/bin/java | xargs -0 dirname
"/usr/lib/jvm/java-11-openjdk-11.0.7.10-1.el8_1.x86_64/bin"

5) Get parent path of /usr/lib/jvm/java-11-openjdk-11.0.7.10-1.el8_1.x86_64/bin/

# readlink -ze /usr/bin/java | xargs -0 dirname | xargs -0 dirname
"/usr/lib/jvm/java-11-openjdk-11.0.7.10-1.el8_1.x86_64"

Posting as answer, as I don't have the privilege to comment.

Point to note: follow the accepted answer posted by "That Dave Guy".

After setting the variables, make sure you set the appropriate permissions to the java directory where it's installed.

chmod -R 755 /usr/java

open kafka-run-class.sh with sudo to write

you can find kafka-run-class.sh in your kafka folder : kafka/bin/kafka-run-class.sh

check for these lines

enter image description here

Modify the JAVA variable in the else part to point to the java executable in your java/bin. like JAVA="$JAVA_HOME/java"


Step 1 - check the current java version by "echo $JAVA_HOME"

Step 2 - vim /etc/profile

Step 3 - At the end of file you will find export JAVA_HOME, we need to provide the new path here, make sure that it is not relative.

Step 4 - Save and exit :wq

Step 5 - "source /etc/profile/", this would execute the change

Step 6 - Again do a echo $JAVA_HOME - change would have been reflected.


Probably a good idea to source whatever profile you edit to save having to use a fresh login.

either: source /etc/ or . /etc/

Where is whatever profile you edited.


The answer is given previous posts is valid. But not one answer is complete with respect to:

  1. Changing the /etc/profile is not recommended simply because of the reason (as stated in /etc/profile):
  • It's NOT a good idea to change this file unless you know what you are doing. It's much better to create a custom.sh shell script in /etc/profile.d/ to make custom changes to your environment, as this will prevent the need for merging in future updates.*
  1. So as stated above create /etc/profile.d/custom.sh file for custom changes.

  2. Now, to always keep updated with newer versions of Java being installed, never put the absolute path, instead use:

#if making jdk as java home

export JAVA_HOME=$(readlink -f /usr/bin/javac | sed "s:/bin/javac::")

OR

#if making jre as java home

export JAVA_HOME=$(readlink -f /usr/bin/java | sed "s:/bin/java::")

  1. And remember to have #! /bin/bash on the custom.sh file

On Linux I add this line to my ~/.profile:

export JAVA_HOME=$(readlink -ze /usr/bin/javac | xargs -0 dirname -z | xargs -0 dirname)

While we are up to setting JAVA_HOME, let me share some benefits of setting JAVA_HOME or any other environment variable:

1) It's easy to upgrade JDK without affecting your application startup and config file which points to JAVA_HOME. you just need to download new version and make sure your JAVA_HOME points to new version of Java. This is best benefit of using environment variable or links.

2) JAVA_HOME variable is short and concise instead of full path to JDK installation directory.

3) JAVA_HOME variable is platform independence i.e. if your startup script uses JAVA_HOME then it can run on Windows and UNIX without any modification, you just need to set JAVA_HOME on respective operating system.

Read more: http://javarevisited.blogspot.com/2012/02/how-to-set-javahome-environment-in.html#ixzz4BWmaYIjH


Use SDKMAN sdkman.io to switch btw. your sdk's.

It sets the JAVA_HOME for you.


Examples related to java

Under what circumstances can I call findViewById with an Options Menu / Action Bar item? How much should a function trust another function How to implement a simple scenario the OO way Two constructors How do I get some variable from another class in Java? this in equals method How to split a string in two and store it in a field How to do perspective fixing? String index out of range: 4 My eclipse won't open, i download the bundle pack it keeps saying error log

Examples related to linux

grep's at sign caught as whitespace How to prevent Google Colab from disconnecting? "E: Unable to locate package python-pip" on Ubuntu 18.04 How to upgrade Python version to 3.7? Install Qt on Ubuntu Get first line of a shell command's output Cannot connect to the Docker daemon at unix:/var/run/docker.sock. Is the docker daemon running? Run bash command on jenkins pipeline How to uninstall an older PHP version from centOS7 How to update-alternatives to Python 3 without breaking apt?

Examples related to java-home

Set ANDROID_HOME environment variable in mac Error: JAVA_HOME is not defined correctly executing maven How to set JAVA_HOME in Linux for all users Android Studio error: "Environment variable does not point to a valid JVM installation" Installing Android Studio, does not point to a valid JVM installation error How can I change Mac OS's default Java VM returned from /usr/libexec/java_home How to set JAVA_HOME path on Ubuntu? Where is the Java SDK folder in my computer? Ubuntu 12.04 How to set JAVA_HOME in Mac permanently? How to set CATALINA_HOME variable in windows 7?

Examples related to path-variables

How to set JAVA_HOME in Linux for all users Error in setting JAVA_HOME How to remove entry from $PATH on mac Is there an equivalent of 'which' on the Windows command line?