3

When I check on maven's version on a virtual environment, I get the following errors:

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

However, I am able to print maven's version outside of virtual environment

mvn -version
Apache Maven 3.5.2 (138edd61fd100ec658bfa2d307c43b76940a5d7d; 2017-10-18T00:58:13-07:00)
Maven home: /usr/local/Cellar/maven/3.5.2/libexec
Java version: 1.8.0_152, vendor: Oracle Corporation
Java home:  /Library/Java/JavaVirtualMachines/jdk1.8.0_152.jdk/Contents/Home/jre
Default locale: en_US, platform encoding: UTF-8
OS name: "mac os x", version: "10.11.6", arch: "x86_64", family: "mac"

I have used set in ~/.bash_profile

set JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_152.jdk
set PATH=$JAVA_HOME/jre/bin:$PATH

I don't understand the error as JAVA_HOME is pointing to a JDK and not a JRE? Thanks for answering!

q2w3e4
  • 195
  • 1
  • 1
  • 7

1 Answers1

6

This is not correct:

set JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_152.jdk

Notice that the output of mvn tells you the location of Java home is /Library/Java/JavaVirtualMachines/jdk1.8.0_152.jdk/Contents/Home/jre. So the location of the JDK home is probably /Library/Java/JavaVirtualMachines/jdk1.8.0_152.jdk/Contents/Home.

Secondly, you need to export the variable, not just set it.

Write like this:

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

After you update your ~/.bash_profile, to test that the setup is correct, start a new shell, and check the output of echo $JAVA_HOME.

janos
  • 120,954
  • 29
  • 226
  • 236
  • 1
    You are the best. That did not catch my eye, I am able to print it now. Thanks. – q2w3e4 Nov 11 '17 at 22:13
  • 1
    As this looks like MacOS I will add that you can more generically set JAVA_HOME with `export JAVA_HOME=$(/usr/libexec/java_home -v1.8)`. This has the added side effect of placing the requested version of Java in the `$PATH`. This is especially useful if you work in an environment that must support multiple versions of Java. – Steve C Nov 12 '17 at 01:24