JAVA_HOME is not working in maven

Viewed 33398

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
6 Answers

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

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

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

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
Related