JAVA_HOME is not working in maven

Amarjit Dhillon picture Amarjit Dhillon · Jul 21, 2017 · Viewed 25.3k times · Source

java is installed at this path

$ which java
/usr/bin/java

mvn -version is giving this error

$ mvn -version
The JAVA_HOME environment variable is not defined correctly
This environment variable is needed to run this program
NB: JAVA_HOME should point to a JDK not a JRE

I have tried some of the solutions that were available online, but those don't work for me. Some of those solutions suggested adding

$export JAVA_HOME = /usr/libexec/java_home 

or

$export JAVA_HOME = $(/usr/libexec/java_home)

to below files

~/.bashrc
~/.bash_profile
~/.profile

Also when I try to execute one shell command, it shows me error like

Error: JAVA_HOME is not defined correctly.
 CARBON cannot execute /System/Library/Frameworks/JavaVM.framework/Versions/CurrentJDK/Home/bin/java

Answer

Amarjit Dhillon picture Amarjit Dhillon · Jul 22, 2017

After struggling for almost a day, I found out that maven is not reading the $JAVA_HOME from

 ~/.bashrc
 ~/.bash_profile
 ~/.profile

but it reads $JAVA_HOME from ~.mavenrc

so finally, when I added

export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_141.jdk/Contents/Home

in ~.mavenrc then got output

mvn -v
Java HotSpot(TM) 64-Bit Server VM warning: ignoring option MaxPermSize=1024m; support was removed in 8.0
Apache Maven 3.5.0 (ff8f5e7444045639af65f6095c62210b5713f426; 2017-04-03T15:39:06-04:00)
Maven home: /usr/local/Cellar/maven/3.5.0/libexec
Java version: 1.8.0_141, vendor: Oracle Corporation
Java home: /Library/Java/JavaVirtualMachines/jdk1.8.0_141.jdk/Contents/Home/jre
Default locale: en_CA, platform encoding: UTF-8
OS name: "mac os x", version: "10.12.6", arch: "x86_64", family: "mac"