Search code examples
javamaveninstallation

mvn -version on command line showing different version of Java


I played around with jenv today, to setup Java 1.8 as global version and 1.7 for one project locally. Since then mvn command in the global locations is showing java version 12 -

SandeepanNath:current sandeepan.nath$ mvn -version
Apache Maven 3.3.9 (bb52d8502b132ec0a5a3f4c09453c07478323dc5; 2015-11-10T22:11:47+05:30)
Maven home: /Users/sandeepan.nath/.sdkman/candidates/maven/current
Java version: 12.0.1, vendor: Oracle Corporation
Java home: /Library/Java/JavaVirtualMachines/jdk-12.0.1.jdk/Contents/Home
Default locale: en_GB, platform encoding: UTF-8
OS name: "mac os x", version: "10.14.3", arch: "x86_64", family: "mac"

Whereas Java version is showing as expected -

SandeepanNath:.ssh sandeepan.nath$ java -version
java version "1.8.0_20"
Java(TM) SE Runtime Environment (build 1.8.0_20-b26)
Java HotSpot(TM) 64-Bit Server VM (build 25.20-b23, mixed mode)

I tried uninstalling and installing maven via sdkman again, on Mac. Although it did not download it fresh. I am still getting the same error.

How do I fix this?


Solution

  • It is possible that your answer might already be here: https://stackoverflow.com/a/7033062/4424668

    I guess, you have set up a JAVA_HOME environment variable at some point, which points to a Java 12-Installation.

    What might be the solution to your specific problem (Jenv + Maven) is written here: https://stackoverflow.com/a/37466252/4424668

    You might need to enable the maven Plugin for Jenv by calling jenv enable-plugin maven. Afterwards, Maven should consider the version activated by Jenv.

    Edit: Since you use SDKMAN for managing maven candidates, you might consider using SDKMAN for Java candidates as well. Though, it is possible the maven plugin won't fix your SDKMAN maven candidates. An even easier solution may be to enable the export-plugin instead, which sets the Environment Variable directly. Have a look at that on the Github-Page of Jenv: https://github.com/jenv/jenv