JAVA_HOME is not working in maven
Asked Answered
H

6

20

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
Heine answered 21/7, 2017 at 6:4 Comment(3)
Have you tried to execute manually /System/Library/Frameworks/JavaVM.framework/Versions/CurrentJDK/Home/bin/java --version? On Mac i would suggest to use ` /Library/Java/JavaVirtualMachines/jdk1.8.0_141.jdk/Contents/‌​Home/bin/java --version` ?Benedict
Somewhat late now, but I suspect that most of the OPs problem would have been caused either by their using spaces around the = when setting the environment value or failing to source the bashrc file after editing.Knighthead
If you use jEnv, you can try jenv enable-plugin maven.Trass
H
50

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"
Heine answered 22/7, 2017 at 20:9 Comment(5)
Thank you. The hint that maven is looking for $JAVA_HOME in the ~/.mavenrc instead of the shell environment variables is correct.Cilo
Tank you so much!! I've been fiddling with this problem for almost two days. :)Tingle
This shouldn't be happening, there is something that needs fixing on the machine. Maven uses JAVA_HOME env as long as it is exported to the environment.Aphesis
I installed maven from homebrew and I don't see .mavenrc in home directory. I created the file and it started working!.Stock
For me, maven did not create a .mavenrc automatically. I had to create it and then add the JAVA_HOME and that did it. Thanks!!Margaret
A
24

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
Amora answered 21/7, 2017 at 6:8 Comment(2)
I have followed all the steps you have said, but I am still getting the same issue. java home path is updated as below $ echo $JAVA_HOME /Library/Java/JavaVirtualMachines/jdk1.8.0_141.jdk/Contents/HomeHeine
Its saying JAVA_HOME should point to a JDK not a JRE. Make sure you have JDK in place not JRE.Muldoon
M
12

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

Myceto answered 24/7, 2018 at 12:42 Comment(1)
It really helped.Sneakers
A
2

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
Ascomycete answered 18/11, 2017 at 23:44 Comment(1)
for LinuxMint (19.3) adding $JAVA_HOME to /etc/environment did not produce any effects. I had to (a) create ~/.mavenrc file (b) add the variable definition to it as JAVA_HOME=/usr/lib/jvm/java-11-openjdk-amd64/ (c) and do source ~/.mavenrc .Vast
V
1

Please check once there should be no ';' in JAVA_HOME value, if there is semicolon then it would show above error.

Veradis answered 23/1, 2019 at 9:40 Comment(0)
M
1

I had

JAVA_HOME

set but did not export it and maven was not picking up the correct JAVA_HOME.

Once I exported JAVA_HOME then it got resolved. Do not forget to export JAVA_HOME so that the variable is available to child processes (UNIX* like OS)

export JAVA_HOME=/Library/Java/JavaVirtualMachines/amazon-corretto-11.jdk/Contents/Home
Maun answered 28/1, 2021 at 23:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.