81

I had OSX with Java 1.6 installed, and I just installed jenv along with Java 1.7:

$ jenv local '1.7'
$ jenv versions
  system
  1.6
  1.6.0.65
* 1.7 (set by /Users/me/workspace/.java-version)
  1.7.0.79
  oracle64-1.6.0.65
  oracle64-1.7.0.79

jenv is doing its job, with $ java -version always working, showing 1.6 when I've set it to 1.6, and 1.7 when I've set it to 1.7:

$ java -version
java version "1.7.0_79"                    <--------------------------------- YAY!!
Java(TM) SE Runtime Environment (build 1.7.0_79-b15)
Java HotSpot(TM) 64-Bit Server VM (build 24.79-b02, mixed mode)

... but maven is ignoring my settings:

$ mvn -version
Apache Maven 3.0.2 (r1056850; 2011-01-08 19:58:10-0500)
Java version: 1.6.0_65, vendor: Apple Inc. <--------------------------------- BAH!!
Java home: /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home
Default locale: en_US, platform encoding: MacRoman
OS name: "mac os x", version: "10.8.5", arch: "x86_64", family: "mac"

I found this SO question where the guy just needed to hardcode his JAVA_HOME inside .mavenrc, but I don't want mine hard-coded (thus jenv!), and I don't have a ~/.mavenrc, nor an /etc/mavenrc.

The version it's using seems to be from whatever's first on the /usr/libexec/java_home output, so in the short term I was able to get 1.6 back again by tweaking 1.7's Info.plist file (from this SO post), but that just means I get 1.6 instead of 1.7.

Any ideas?

Community
  • 1
  • 1
inanutshellus
  • 9,683
  • 9
  • 53
  • 71

6 Answers6

191

You need to install the jenv maven plugin, try the following command and reload your shell:

jenv enable-plugin maven

tenfourty
  • 2,111
  • 1
  • 13
  • 5
  • 1
    Not sure why it took me so long to notice this answer. This works like a charm! Much better than having to prefix everything. Thanks (even if belatedly)! – inanutshellus Sep 27 '17 at 03:03
  • Thank you, @tenfourty! This really helped. This worked for me when switching between different versions of Java for building projects with multiple JDKs for testing. – cspider Jun 03 '19 at 22:36
  • 1
    I did have this enabled but it didn’t work. Turns out, jenv (as installed by Homebrew) uses a symlink in ~/.jenv/plugins and this pointed to an old jenv version. Fixed the link, fixed jenx, fixed Maven! – Bombe Jun 17 '19 at 11:49
  • I needed to run jenv exec mvn -version. Only enabling the plugin didn't work for me. – Sandeepan Nath Jun 09 '20 at 17:02
  • 2
    I had issues after a Homebrew (macOS) upgrade today where Maven wasn't respecting the configured JVM version. Not sure if a rehash and shell reload would have taken care of it, but disabling and reenabling the Maven plugin fixed it for sure. – jocull Jul 14 '20 at 16:28
  • Setting up a new machine today I ran into an issue where `jenv enable-plugin` resulted in `no such command`. Addressed by running `jenv doctor` and resolving the issues found as suggested by https://github.com/jenv/jenv/issues/64#issuecomment-64396967 – jocull May 10 '22 at 17:49
  • This fixed my problem a few months ago, but now i ran into the same problem again without having changed anything. Maven was ignoring jenv even though the maven plugin was activated in jenv. Disabling and re-enabling didn't help either. I also had to enable the export plugin to fix it this time: `jenv enable-plugin export` – rzueger Aug 17 '22 at 08:31
32

I had a similar problem. I got things running by prefixing all command with jenv exec:

jenv exec mvn -version
aautio
  • 321
  • 3
  • 3
19

Now, there's a new option how to set JAVA_HOME via jenv export plugin:

jenv enable-plugin export

See https://github.com/gcuisinier/jenv/issues/44#issuecomment-233124185

Juraj Martinka
  • 3,991
  • 2
  • 23
  • 25
5

If you have enabled the maven and export plugins, and still have problems, check that you don't have a ~/.mavenrc file which is setting JAVA_HOME.

tgdavies
  • 10,307
  • 4
  • 35
  • 40
2

If jenv enable-plugin export still doesn't work then you should restart the Terminal and try again

  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Aug 22 '22 at 11:29
0

I use pwsh on OS X and was able to get it working with the maven and export plugins enabled.

However, I encountered the following gotchas:

  • Run sh-enable-plugin instead of enable-plugin when enabling maven and export (enable-plugin produces a "command not found" error).
  • Edit the mvn script (code "$(which mvn)") and remove the JAVA_HOME= bit so that you end up with something like this:
#!/bin/bash
exec "/usr/local/Cellar/maven/3.8.5/libexec/bin/mvn"  "$@"

These additional steps fixed my issues so that mvn -v prints the correct JDK version.

Connor Low
  • 5,900
  • 3
  • 31
  • 52