JAVA_HOME is not working in maven

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

 ~/.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"

maven reads JAVA_HOME from ~/.mavenrc. Add path for JAVA_HOME to ~./mavenrc and source it.

$ vim ~/.mavenrc

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

$ source .mavenrc


To fixed it, update the JAVA_HOME like following :

$ vim .bash_profile

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

$ source .bash_profile

Run Maven again :

$ mvn -version

On Ubuntu I faced a similar problem. I configured $JAVA_HOME in /etc/environment like JAVA_HOME=PATH_TO_JDK for example JAVA_HOME=/home/max/jdk1.8.0_144

Careful with

  • White space after path declaration JAVA_HOME=/home/max/jdk1.8.0_144[[_NO_WHITE_SPACE_AFTER_DECLARATION]]
  • Don't put any quotes, e.g. JAVA_HOME="/home/max/jdk1.8.0_144"
  • Don't put /bin, e.g. JAVA_HOME=/home/max/jdk1.8.0_144/bin <- This is wrong