23

How to get the java home dir?

When doing this

echo $JAVA_HOME

Nothing is returned

enter image description here

clarkk
  • 27,151
  • 72
  • 200
  • 340
  • 1
    The answer is here: https://stackoverflow.com/questions/24641536/how-to-set-java-home-in-linux-for-all-users – Michu93 Feb 21 '18 at 10:02

8 Answers8

45

You need to set the $JAVA_HOME variable

In my case while setting up Maven, I had to set it up to where JDK is installed.

First find out where JAVA is installed:

$ whereis java
java: /usr/bin/java /usr/share/java /usr/share/man/man1/java.1.gz

Now dig deeper-

$ ls -l /usr/bin/java
lrwxrwxrwx 1 root root 46 Aug 25  2016 /etc/alternatives/java -> /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java

Dig deeper:

$ ls -l /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java
-rwxr-xr-x 1 root root 6464 Mar 14 18:28 /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java

As it is not being referenced to any other directory, we'll use this.

Open /etc/environment using nano

$ sudo nano /etc/environment

Append the following lines

JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk-amd64
export JAVA_HOME

Reload PATH using $ . /etc/environment

Now, $ echo $JAVA_HOME

Here is your output:

/usr/lib/jvm/java-1.8.0-openjdk-amd64

Sources I referred to:

https://askubuntu.com/a/175519

https://stackoverflow.com/a/23427862/6297483

Sahil Khurana
  • 478
  • 4
  • 10
  • 1
    Excellent answer. I know the original poster didn't mark your answer as correct, but the detailed instructions worked for me. Thank you. – Longblog Sep 18 '19 at 16:23
  • 1
    does not work, https://stackoverflow.com/questions/24641536/how-to-set-java-home-in-linux-for-all-users has the correct answer – Payel Senapati Nov 27 '20 at 19:16
  • Upon reboot, the /etc/environment variables are NOT loaded. This is not a workable solution. – JeremyP Apr 21 '23 at 12:43
9

$JAVA_HOME is a global variable that you typically must set yourself.

In certain (most?) platforms, installing Java will not set your JAVA_HOME variable.

The advantage here is that you can have multiple Java versions co-existing within one system.

Since you're running on *nix system, you can do that in your own logon scripts, such as ~/.bashrc or ~/.bash_profile, etc.

Mena
  • 47,782
  • 11
  • 87
  • 106
3
  1. Open your terminal, and open your .bash_profile file by executing:

    nano ~/.bash_profile

(If it is the first time, it should be empty) 2. Ad an echo message just to see a greeting message by pasting

echo "Hello, Your Bash Profile Is Running…" 

Type: ctrl + x to exit from nano Type: Y to save change and press enter

  1. In order to get the message to appear every time we open a terminal execute: source ~/.bash_profile
  2. In the file ~/.bash_profile, set the $JAVA_HOME environment variable by adding the following to the end of the file:

    export JAVA_HOME="/Library/Java/JavaVirtualMachines/jdk13.jdk/Contents/Home" export PATH="$JAVA_HOME/bin:$PATH"

Type: ctrl + x to exit from nano Type: Y to save change and press enter

  1. Close and reopen your terminal to update the environment variables.

  2. Verify JAVA_HOME by executing echo $JAVA_HOME.

Source: https://www.youtube.com/watch?v=0gceUrMnjzo

Thiago Farias
  • 301
  • 2
  • 7
2

If the terminal is zsh and not bash then we need to set JAVA_HOME in either of these files ~/.zshenv or ~/.zshrc more details about zsh terminal here

steps:

  • run this command in the terminal open ~/.zshrc
  • in the zshrc file append java_home path (refer below two lines)
  • export JAVA_HOME="/Library/Java/JavaVirtualMachines/jdk-x.x.x.jdk/Contents/Home"
  • export PATH="$JAVA_HOME/bin:$PATH"}
  • re-launch zsh terminal
  • In the zsh terminal run this command echo $JAVA_HOME
Jlearner
  • 573
  • 4
  • 6
1

If you are using latest versions of OS, then you cannot use ~/.bash_profile to export your environment variable since bash shell is deprecated in the latest version of OS.

This answer may give you the exact solution:https://stackoverflow.com/a/62192114/3962688

Ilham Safeek
  • 217
  • 1
  • 5
  • 15
0

What solved to me, using Ubuntu 18, was removing java from /etc/profile

So, export JAVA_HOME=/usr/bin

nanquim
  • 1,786
  • 7
  • 32
  • 50
0

after saving JAVA_HOME in enviroment file

$ source /etc/environment $ echo $JAVA_HOME

this worked for me

kpgan8204
  • 91
  • 1
  • 3
-2

set it first?

export JAVA_HOME=/usr/bin/java

See more here:

How to set JAVA_HOME in Linux for all users

Community
  • 1
  • 1
JavaSheriff
  • 7,074
  • 20
  • 89
  • 159