288

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?

hnk
  • 2,216
  • 1
  • 13
  • 18
Bosco
  • 3,835
  • 6
  • 25
  • 33

25 Answers25

527
  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
Robin Green
  • 32,079
  • 16
  • 104
  • 187
That Dave Guy
  • 5,512
  • 1
  • 11
  • 16
  • 43
    don't forget to delete the double quotes and recreate them from your keyboard, because only copying and pasting may create troubles. – Sunil Kumar Jan 13 '15 at 15:43
  • I do believe you can skip the "export JAVA_HOME" part and simply do an "export PATH=$PATH:/path_to_java/bin". I haven't had a problem so far when not defining JAVA_HOME but maybe it's just pure luck on my part. – rbaleksandar Mar 04 '15 at 18:40
  • 18
    @rbaleksandar some applications depend on `JAVA_HOME`, doesn't hurt setting it up too. – raffian Jul 27 '15 at 02:15
  • Maybe, never encountered such an application. – rbaleksandar Jul 27 '15 at 08:18
  • 4
    IntelliJ is such an application - and not a minor one. – Pieter van den Ham Aug 02 '15 at 00:34
  • 24
    You need to run `source /etc/profile` for changes to take effect immediately too! – Mohamed Taher Alrefaie Jun 24 '16 at 08:57
  • printenv JAVA_HOME to check the result afterward – G_V Dec 10 '17 at 14:33
  • Make sure you don't already have these options in the file before you add them. – Brent Sandstrom Mar 09 '18 at 17:51
  • this is the right solution. In the next reboot changes picked automatically as expected. if you do the same changes in /etc/environment then changes will be for current session only. /etc/profile is right place to update paths permanently. – Rajesh Mbm Mar 24 '18 at 18:49
  • This doesn't work for my tomcat user. It can't resolve `$JAVA_HOME ` in its `/etc/init.d` startup script when set in this way. – 8bitjunkie May 29 '18 at 09:49
  • 7
    I think it is safe to mention how to save the updates you make by going into command mode by Pressing "Esc" and then typing ":w" and pressing "Enter". – RocketRuwan Feb 01 '19 at 09:47
  • 1
    This pointed me to the right direction. There actually was a stale `/etc/profile.d/jdk.sh` floating around in my system. – Hermann Dec 12 '19 at 22:18
  • This method does not works if you are trying to make java available to a systemd service, `systemctl start `. For that Use `Enviornment="JAVA_HOME=path_to_java" ` in your `.service` file. – Talha Asghar Dec 07 '21 at 08:40
  • you can use```update-java-alternatives -l``` shows you all the Java versions you have installed. alternative to this ```find /usr/lib/jvm/java-1.x.x-openjdk``` – leonidaa Mar 14 '22 at 14:55
  • @TalhaAsghar, I am using linux aarch64 architecture. I am installing tensorflow from the bazel file. When I try to compile the bazel ./compile.sh, I got ```ERROR: JAVA_HOME (/usr/lib/jvm/java-11-openjdk-arm64/bin/java) is not a path to a working JDK.```. I follow your steps. ```printenv JAVA_HOME``` gives ```/usr/lib/jvm/java-11-openjdk-arm64/bin/java```. Because ```ls -l /etc/alternatives/java``` gives```lrwxrwxrwx 1 root root 43 Aug 18 11:47 /etc/alternatives/java -> /usr/lib/jvm/java-11-openjdk-arm64/bin/java``` – Susan Aug 18 '22 at 07:59
  • But after rebooting the OS and compile the ./compile.sh file from the bazel folder, I got the same error of ```ERROR: JAVA_HOME (/usr/lib/jvm/java-11-openjdk-arm64/bin/java) is not a path to a working JDK.``` May I know how can I solve the issue? – Susan Aug 18 '22 at 08:00
  • @Susan, it looks like the only problem is that your `JAVA_HOME` variable has been set an *actual java executable file*, rather than the Java installation directory. It's this installation directory that various applications will look to find all the *java stuff* they need. Perhaps "path_to_java" is misleading. Try this assignment `JAVA_HOME=/usr/lib/jvm/java-11-openjdk-arm64` for your architecture. – adebayo10k Aug 19 '22 at 04:57
  • @adebayo10k, thank you for advice. The issue is solved by setting ```JAVA_HOME=/usr/lib/jvm/java-11-openjdk-arm64/``` – Susan Aug 19 '22 at 05:42
  • I like how you give specific instruction to get to insert mode for people who don't know how to use vim and then give no instructions how to exit it :) – superpupervlad Aug 19 '23 at 10:48
174

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.

Eero Aaltonen
  • 4,239
  • 1
  • 29
  • 41
  • 24
    to comply with just plain JRE (even headless) use `export JAVA_HOME=$(readlink -f /usr/bin/java | sed "s:/bin/java::")` instead - notice that I use just `java`, not `javac` – Roman Kruglov Feb 27 '17 at 15:55
  • @occulta At least [Maven](http://maven.apache.org/install.html) expects JAVA_HOME to point to a JDK and uses it to locate the java compiler. – Eero Aaltonen Feb 28 '17 at 09:33
  • 1
    Upvote for `readlink` and update/upgrade compatible solution, even this is an 5 years old thread. I only recommend not to edit /etc/profile, but place your export inside custom file, e.g. `/etc/profile.d/java_environment.sh`, maybe you have to `chmod +x java_environment.sh` and reboot. – ChristophS Jul 19 '19 at 11:37
  • Perfect. Better than my clunky `dirname`-ing solution. If you're like me and wanting to understand what's going on here, this is a simple replacement of matching text from input with an empty string. The "default" character for replacements is the `/`, but as long as you're consistent, you can replace the `/` with anything. In this case it's colons as we use `/` for path separators. – Ungeheuer Aug 14 '19 at 00:08
45

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.

flob
  • 3,760
  • 2
  • 34
  • 57
  • 1
    at least in my linux (raspbian), /etc/profile will source `/etc/profile.d/*.sh` so your file needs to be called `jdk_home.sh` so it gets sourced – Hilikus Jul 19 '17 at 03:49
  • Thanks @Hilikus :-) I changed it accordingly. – flob Jul 19 '17 at 06:34
  • 2
    Thank you. i did a mix of your and @Eero's answer for the best of both worlds ;) – Hilikus Jul 19 '17 at 15:15
  • 1
    `/etc/profile.d/jdk_home.sh` is the much more clean answer, at least for **Ubuntu** , `/etc/profile` is clumped with so much logic already. Didn't seems wise to add more onto it... – Ng Sek Long Sep 27 '19 at 08:55
38

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/
Kyle Falconer
  • 8,302
  • 6
  • 48
  • 68
  • I had the same experience on RHEL 7. I removed the exports from the ~/.bash_profile and used this approach. – xpagesbeast Jun 25 '18 at 15:31
  • #!/bin/sh is not required in your jdk_home.sh. once you done the configuration make sure to logout and login again – kuhajeyan Aug 14 '19 at 10:11
  • Also, using the default jvm symlink for java_home is probably good https://stackoverflow.com/questions/663658/what-is-the-correct-target-for-the-java-home-environment-variable-for-a-linux-op – Alkanshel May 01 '22 at 00:19
36
  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

Zoe
  • 27,060
  • 21
  • 118
  • 148
PyDevSRS
  • 1,715
  • 16
  • 17
15

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
frido
  • 13,065
  • 5
  • 42
  • 56
VirangaMahesh
  • 325
  • 1
  • 4
  • 12
9

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
alok ailawadi
  • 395
  • 1
  • 6
  • 18
9

First you need to find out which Java is installed in your PC and which one to use. For that open terminal with root permission.

 sudo su

 ls /usr/lib/jvm/

Now it will list the available java versions. Select the listed version.

Copy the path till there.

Now open bashrc

  nano ~/.bashrc

add the following commands to the end

 export JAVA_HOME="path that you copied"

  export PATH=$JAVA_HOME/bin:$PATH

after that save the file and exit by pressing Ctrl+S followed by Ctrl+X

Now run the below command:

  source ~/.bashrc
Amal K V
  • 91
  • 1
  • 1
8

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

Steve S
  • 91
  • 1
  • 1
5

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

Ng Sek Long
  • 4,233
  • 2
  • 31
  • 38
4

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
Dylan Breugne
  • 455
  • 4
  • 7
4

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
Van Peer
  • 2,127
  • 2
  • 25
  • 35
4

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"
ahmet
  • 1,085
  • 2
  • 16
  • 32
3

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.

swapnil shashank
  • 877
  • 8
  • 11
2

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.

2

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

export JAVA_HOME=$(readlink -ze /usr/bin/javac | xargs -0 dirname -z | xargs -0 dirname)
  • 2
    It seems a potentially correct answer but could you explain *why* it works, ie what it does and what the OP's problem is? Also, you say "on Linux", but there are many different Linux distro's and it might not work for all of them, please add for which distro this works. – Buurman Nov 04 '15 at 12:01
2

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

guest
  • 91
  • 1
  • 1
2

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

It sets the JAVA_HOME for you.

Stefan Höltker
  • 311
  • 5
  • 21
2

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"

2

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.

Noha
  • 59
  • 3
0

Using vim might be a bit difficult for new user. We can use gedit text editor instead.

  1. Find /usr/lib/jvm/java-1.x.x-openjdk

  2. Enter "gedit /etc/profile" or use "sudo gedit /etc/profile" if logged in as not-privileged

  3. Add the following at the end of line:

    export JAVA_HOME="path that you found"

    export PATH=$JAVA_HOME/bin:$PATH

  4. Enter "source /etc/profile" in your current shell to apply the changes

Jauhari
  • 13
  • 2
0

export JAVA_HOME="/usr/lib/jvm/jdk-*" solves the issue

PKS
  • 618
  • 1
  • 7
  • 19
0

AND also i had problem if someone else export PATH in other script, in my case in .bashrc there was line for sdkman

export PATH=/home/ars/.sdkman/candidates/gradle/current/bin:/home/ars/.nvm/versions/node/v15.14.0/bin:/home/ars/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin

So when i run echo $JAVA_HOME there was a result but when a run echo $PATH there was no $JAVA_HOME/bin

-1

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.

darkhipo
  • 1,384
  • 1
  • 14
  • 19
-3

Try this if doesn't work:

apt install openjdk-8-jdk-headless
Ivan B
  • 1
  • 1