Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JAVA_HOME is not working in maven

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
like image 599
Amarjit Dhillon Avatar asked Jul 21 '17 06:07

Amarjit Dhillon


4 Answers

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"
like image 104
Amarjit Dhillon Avatar answered Sep 20 '22 14:09

Amarjit Dhillon


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
like image 23
Anshul Sharma Avatar answered Sep 23 '22 14:09

Anshul Sharma


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

like image 38
Tariq Kamal Avatar answered Sep 21 '22 14:09

Tariq Kamal


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
like image 44
Adelin Avatar answered Sep 23 '22 14:09

Adelin