Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

echo $JAVA_HOME returns nothing

Tags:

java

How to get the java home dir?

When doing this

echo $JAVA_HOME

Nothing is returned

enter image description here

like image 995
clarkk Avatar asked Mar 02 '16 16:03

clarkk


People also ask

What should echo $Java_home return?

The echo $JAVA_HOME command-line must return the complete path directory to java executable.

How do you check JAVA_HOME is set or not?

Verify JAVA_HOMEOpen a Command Prompt window (Win⊞ + R, type cmd, hit Enter). Enter the command echo %JAVA_HOME% . This should output the path to your Java installation folder. If it doesn't, your JAVA_HOME variable was not set correctly.

What should JAVA_HOME be set to Windows?

To set JAVA_HOME, do the following: Right click My Computer and select Properties. On the Advanced tab, select Environment Variables, and then edit JAVA_HOME to point to where the JDK software is located, for example, C:\Program Files\Java\jdk1. 6.0_02.


2 Answers

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

like image 176
Sahil Khurana Avatar answered Sep 16 '22 22:09

Sahil Khurana


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

like image 39
Mena Avatar answered Sep 20 '22 22:09

Mena