How to get the java home dir?
When doing this
echo $JAVA_HOME
Nothing is returned
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:
$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.
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
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
Close and reopen your terminal to update the environment variables.
Verify JAVA_HOME by executing echo $JAVA_HOME.
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:
open ~/.zshrc
export JAVA_HOME="/Library/Java/JavaVirtualMachines/jdk-x.x.x.jdk/Contents/Home"
export PATH="$JAVA_HOME/bin:$PATH"}
echo $JAVA_HOME
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
What solved to me, using Ubuntu 18, was removing java from /etc/profile
So, export JAVA_HOME=/usr/bin
after saving JAVA_HOME in enviroment file
$ source /etc/environment $ echo $JAVA_HOME
this worked for me
set it first?
export JAVA_HOME=/usr/bin/java
See more here: