Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JAVA_HOME not set on apple m1 #339

Open
EeeMt opened this issue Oct 20, 2021 · 9 comments
Open

JAVA_HOME not set on apple m1 #339

EeeMt opened this issue Oct 20, 2021 · 9 comments

Comments

@EeeMt
Copy link

EeeMt commented Oct 20, 2021

jenv 0.5.4

I execute follow steps on my m1 MBA and intel MBP:

1. brew install jenv
2. echo eval "$(jenv init -)" >> /Users/xiquan/.zshrc
3. jenv add $(/usr/libexec/java_home)
4. jenv global 1.8
5. <open new terminal tab>
6. echo ${JAVA_HOME}

The intel MBP works fine while the m1 MBA not.

Environment info of the m1 MBA:

Last login: Wed Oct 20 22:42:00 on ttys008
➜  ~ jenv --version
jenv 0.5.4
➜  ~ jenv doctor
[OK]	No JAVA_HOME set
[OK]	Java binaries in path are jenv shims
[OK]	Jenv is correctly loaded
➜  ~ echo $PATH
/Users/xiquan/.jenv/shims:/Users/xiquan/.jenv/bin:/opt/homebrew/bin:/opt/homebrew/sbin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/opt/homebrew/opt/fzf/bin
➜  ~ grep -v '#' .zshrc | grep -ve '^$'
export ZSH="/Users/xiquan/.oh-my-zsh"
ZSH_THEME="robbyrussell"
plugins=(git zsh-autosuggestions)
source $ZSH/oh-my-zsh.sh
export PATH="$HOME/.jenv/bin:$PATH"
[ -f ~/.fzf.zsh ] && source ~/.fzf.zsh
eval export PATH="/Users/xiquan/.jenv/shims:${PATH}"
export JENV_SHELL=zsh
export JENV_LOADED=1
unset JAVA_HOME
source '/opt/homebrew/Cellar/jenv/0.5.4/libexec/libexec/../completions/jenv.zsh'
jenv rehash 2>/dev/null
jenv refresh-plugins
jenv() {
  typeset command
  command="$1"
    shift
  fi
  case "$command" in
  enable-plugin|rehash|shell|shell-options)
    eval `jenv "sh-$command" "$@"`;;
  *)
    command jenv "$command" "$@";;
  esac
}
➜  ~ echo $JAVA_HOME~ ls .jenv/plugins
export gradle maven
➜  ~ jenv enable-plugin export
export plugin already enabled
➜  ~ jenv version
11 (set by /Users/xiquan/.java-version)
➜  ~ jenv versions
  system
  1.8
  1.8.0.302
* 11 (set by /Users/xiquan/.java-version)
  11.0
  11.0.12
  17
  openjdk64-11.0.12
  zulu64-1.8.0.302
  zulu64-17
➜  ~ command -v java
/Users/xiquan/.jenv/shims/java
➜  ~
@kito99
Copy link

kito99 commented Dec 7, 2021

I was able to get this working by doing the following:

rm ~/Library/LaunchAgents/jenv-environment.plist
jenv macos-javahome

To view the file:

cat ~/Library/LaunchAgents/jenv-environment.plist
<?xml version="1.0" encoding="UTF-8"?>
 <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
 <plist version="1.0">
 <dict>
   <key>Label</key>
   <string>my.startup</string>
   <key>ProgramArguments</key>
   <array>
     <string>sh</string>
     <string>-c</string>
     <string>
     launchctl setenv JAVA_HOME /usr/local/opt/jenv/versions/11.0 ;
     </string>
   </array>
   <key>RunAtLoad</key>
   <true/>
 </dict>
 </plist>

Verify in a new shell:

interneuron:~ kito$ echo $JAVA_HOME
/usr/local/opt/jenv/versions/11.0

If you're using an IDE or another tool, you'll have to restart it. (This worked for me when using IntelliJ and spawning Maven).

@richardbadman
Copy link

richardbadman commented Mar 1, 2022

I'm having the same issue,

~ ❯ jenv doctor                                                                                                                                                                                                                              ⏎
[OK]	No JAVA_HOME set
[OK]	Java binaries in path are jenv shims
[OK]	Jenv is correctly loaded
~ ❯ jenv versions
  system
  1.8
  1.8.0.292
  11
  11.0
* 11.0.12 (set by /Users/<username>/.java-version)
  openjdk64-1.8.0.292
  openjdk64-11.0.12
~ ❯ jenv enable-plugin export
export plugin already enabled
~ ❯ jenv local 1.8
~ ❯ exec $SHELL -l
~ ❯ cat .java-version
1.8
~ ❯ echo $JAVA_HOME

~ ❯ echo ${JAVA_HOME}

~ ❯

With the following being in my ~/.zshrc

# jenv
eval export PATH="/Users/<username>/.jenv/shims:${PATH}"
export JENV_SHELL=zsh
export JENV_LOADED=1
unset JAVA_HOME
source '/opt/homebrew/Cellar/jenv/0.5.4/libexec/libexec/../completions/jenv.zsh'
jenv rehash 2>/dev/null
jenv refresh-plugins
jenv() {
  typeset command
  command="$1"
  if [ "$#" -gt 0 ]; then
    shift
  fi

  case "$command" in
  enable-plugin|rehash|shell|shell-options)
    eval `jenv "sh-$command" "$@"`;;
  *)
    command jenv "$command" "$@";;
  esac
}

I didn't actually have the file /Library/LaunchAgents/jenv-environment.plist, and running jenv macos-javahome gives the following;

~ ❯ jenv macos-javahome
/opt/homebrew/Cellar/jenv/0.5.4/libexec/libexec/jenv-macos-javahome: line 13: /Users/<username>/Library/LaunchAgents/jenv-environment.plist: No such file or directory
Unload failed: 5: Input/output error
Try running `launchctl bootout` as root for richer errors.
Load failed: 5: Input/output error
Try running `launchctl bootstrap` as root for richer errors.

@jmayday
Copy link

jmayday commented Apr 19, 2022

Exactly same issue on my MacBook Pro M1. Because of that Maven is using incorrect version of Java (I need it to pick up Java 11):

mayday@MacBook-Pro-j ~ % jenv versions
  system
* 11 (set by /Users/mayday/.java-version)
  11.0
  11.0.15
  16
  16.0
  16.0.2
  openjdk64-11.0.15
  openjdk64-16.0.2

mayday@MacBook-Pro-j ~ % java -version
openjdk version "11.0.15" 2022-04-19
OpenJDK Runtime Environment Homebrew (build 11.0.15+0)
OpenJDK 64-Bit Server VM Homebrew (build 11.0.15+0, mixed mode)

mayday@MacBook-Pro-j ~ % mvn -version
Apache Maven 3.8.5 (3599d3414f046de2324203b78ddcf9b5e4388aa0)
Maven home: /opt/homebrew/Cellar/maven/3.8.5/libexec
Java version: 18, vendor: Homebrew, runtime: /opt/homebrew/Cellar/openjdk/18/libexec/openjdk.jdk/Contents/Home
Default locale: pl_PL, platform encoding: UTF-8
OS name: "mac os x", version: "12.3.1", arch: "aarch64", family: "mac"

mayday@MacBook-Pro-j ~ %

@dusiema
Copy link

dusiema commented Apr 25, 2022

Same problem here on a M1 Mac.

Using Kotlin Multiplatform Build which runs from xCode with a run script.
I ended up editing ~/Library/LaunchAgents/jenv-environment.plist and manually setting JAVA_HOME to make the script working.

@tudorvirga
Copy link

Got the same problem and I got it fixed by running: jenv rehash followed by a exec $SHELL -l. Hope it helps someone.

@jebeaudet
Copy link

For me, I fixed it just by running jenv enable-plugin export and by restarting my shell. Hopefully it helps someone
Source: https://github.com/albert-hg/Mac-M1-First-Setting-Guide/blob/main/README.md#jenv

@vashisth00
Copy link

Same issue jenv: version `ibm64-1.8.0.302' is not installed

@kpitt
Copy link

kpitt commented Nov 1, 2022

I was having the same problem here, but not with a GUI application so I knew ~/LibraryLaunchAgents/jenv-environment.plist wasn't the problem. For me, jenv wasn't setting JAVA_HOME in the shell, either on startup or when changing to a directory containing a local .java-version file. I checked that jenv was being initialized in my startup scripts, that the export plugin was enabled, and that jenv javahome was reporting the correct directory, but I still had no JAVA_* or JDK_* environment variables.

After banging my head against the wall for way too long, I finally figured out that the init-caching I use in my shell configuration had cached the jenv init - statements before I enabled the export plugin. Even though jenv itself was getting initialized, the line that should have source'd the export hook function was missing. After updating the cached init to include the proper export plugin initialization, everything now works as expected.

@glesirok
Copy link

glesirok commented Jan 11, 2023

MacBook Air M1.

you also need to exec:
echo 'export PATH="$HOME/.jenv/bin:$PATH"' >> ~/.zshrc
The PATH env variable is what you missed.
Source:https://www.jenv.be/

Then enable the export plugin:
jenv enable-plugin export
exec $SHELL -l

I found that if PATH is not set correctly, jenv wouldn't set JAVA_HOME even if the plugin is enabled

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

10 participants