JAVA_HOME is not defined correctly (set it correctly)

If you get this while running java based sw (for ex: maven)

$ mvn
Error: JAVA_HOME is not defined correctly.
We cannot execute /usr/bin/java/bin/java

 

add the java home and java path (in my case, oracle java 8) to the enviroment variables (ex, with text editor vim)

sudo vim /etc/profile
#add these 2 lines at the end of the file:
export JAVA_HOME="/usr/lib/jvm/java-8-oracle"
export PATH=JAVA_HOME/bin:$PATH

save and reload vars executing:

$ source /etc/profile

all done. Now trying one more time:

$mvn -version
Apache Maven 3.0.5
Maven home: /usr/share/maven
Java version: 1.8.0_66, vendor: Oracle Corporation
Java home: /usr/lib/jvm/java-8-oracle/jre