4

I have jenv installed. The output of which java was initially /Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/bin/java. I issues the following commands:

jenv add /Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home
eval "$(jenv init -)"

Now, the output of which java is

/Users/my_username/.jenv/shims/java

The output of java -version is the same before and after. However, mvn clean install is throwing error:

Error: JAVA_HOME is not defined correctly.
  We cannot execute /Users/my_username/.jenv/versions/system/bin/java

Why is maven not picking up the proper java?

dknight
  • 1,243
  • 10
  • 23
  • 2
    Can you echo $JAVA_HOME on the terminal? Does it point to a valid location? – J_D Jan 08 '19 at 10:19
  • Can you run java directly from console? – talex Jan 08 '19 at 10:20
  • $JAVA_HOME is empty. I am able to run java -version. – dknight Jan 08 '19 at 10:22
  • 1
    Have you tried the `$ jenv enable-plugins maven` and `$ jenv enable-plugins export`? – J_D Jan 08 '19 at 10:35
  • I am getting "jenv: no such command `enable-plugins'" as the message when I tried to enable maven plugin. – dknight Jan 08 '19 at 10:37
  • Perhaps restart your terminal? – J_D Jan 08 '19 at 10:39
  • Restarting the terminal has the same problem – dknight Jan 08 '19 at 10:52
  • 2
    add this to .bashrc or .zshrc `export JAVA_HOME=$(/usr/libexec/java_home)` and restart terminal, you can check first does it works by executing above command on terminal then update files. – dkb Jan 08 '19 at 11:22
  • @dkb Near as I can tell, setting `JAVA_HOME` in bashrc _seems_ help `jenv` establish where to find the system default; without that some things just don't work. `jenv global system && java -version` works fine; but a play build didn't like it at all; it worked just fine with `jenv global system` if I had JAVA_HOME set in bashrc. – Richard Sitze Nov 18 '21 at 18:55
  • @J_D it's "`jenv enable-plugin`", not "enable-plugin**s**" – golwig Mar 14 '23 at 11:52

2 Answers2

10

According to documentation eval "$(jenv init -)" should print to console some command which you need to execute (they suggest to add them to .bash_profile).

talex
  • 17,973
  • 3
  • 29
  • 66
  • @PapaStan Yes. And those commands produce new commands. Here is instruction on how to install from manual `echo 'eval "$(jenv init -)"' >> ~/.bash_profile` – talex Jan 08 '19 at 10:33
  • The documentation says that jenv init enables shims and autocompletion. – PapaStan Jan 08 '19 at 10:38
  • @PapaStan maybe it inits JAVA_HOME too? – talex Jan 08 '19 at 10:38
  • 1
    @dknight no. It shows what else you have to do to make it work. You have to put those commands in your init scripts. – talex Jan 08 '19 at 11:01
  • good info, but does not address the root problem. – Richard Sitze Nov 18 '21 at 18:26
  • @RichardSitze you mean executing command printed by `eval "$(jenv init -)"` doesn't work? – talex Nov 20 '21 at 12:45
  • 1
    @Talex Not in my situation. Still trying to wrap my head around my particular root-cause. Basically, of `java` isn't on `PATH` (not including `jenv` shims) then it cannot find a system/default `java` to run, and I get the "We cannot execute /Users/my_username/.jenv/versions/system/bin/java" error described by OP. The jenv init/setup in itself doesn't "fix" an underlying problem. The jenv docs do not really call out how the default is determined, so I started digging into the jenv impl to work things out. – Richard Sitze Nov 21 '21 at 07:08
  • This seemed to fix my issues when adding new JAVA versions. Thanks. – Peter Suwara Mar 15 '23 at 03:09
0

jenv versions showed the current version was system. I am not sure why that version was not proper. I set the global version to 1.8.0.121 and everything is working as expected.

dknight
  • 1,243
  • 10
  • 23