As of January 25, 2020, other Qiita articles are old and do not work properly & are different from the official document, so I will describe it
Confirmed with macOS Mojave 10.14.6
mkdir ~/.jenv
brew install jenv
git clone https://github.com/jenv/jenv.git ~/.jenv
2-1. bash
echo 'export PATH="$HOME/.jenv/bin:$PATH"' >> ~/.bash_profile
echo 'eval "$(jenv init -)"' >> ~/.bash_profile
2-2. zsh
echo 'export PATH="$HOME/.jenv/bin:$PATH"' >> ~/.zshrc
echo 'eval "$(jenv init -)"' >> ~/.zshrc
manner
jenv doctor
If it works properly, the following output will be output
[OK] No JAVA_HOME set
[ERROR] Java binary in path is not in the jenv shims.
[ERROR] Please check your path, or try using /path/to/java/home is not a valid path to java installation.
PATH : /Users/user/.jenv/libexec:/Users/user/.jenv/shims:/Users/user/.jenv/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin
[OK] Jenv is correctly loaded
echo ${JAVA_HOME}
jenv enable-plugin export
exec $SHELL -l
When installing Java environment with brew cask
brew cask install java
For Java 8
brew cask install java8
Use jenv add
jenv add $(/usr/libexec/java_home)
Of course, you may specify the directory directly as follows.
jenv add /Library/Java/JavaVirtualMachines/jdk1.8.0_241.jdk/Contents/Home
$ jenv versions
* system (set by /Users/user/.jenv/version)
11.0
11.0.2
openjdk64-11.0.2
By default, system Java is the latest version of Java.
$ jenv local 11.0.2
$ exec $SHELL -l
$ cat .java-version
11.0.2
echo ${JAVA_HOME}
/Users/hogehoge/.jenv/versions/11.0.2
Setting is OK with this.
rm .java-version
Required only when you want to set it globally
jenv global 11.0.2
jenv shell 11.0.2
Install Java 8 where Java 11.0.2 is installed
brew cask install adoptopenjdk8
brew cask install caskroom/versions/adoptopenjdk8
Install the latest version of Java 8 in a special directory on macOS by the above
$ ls -1 /Library/Java/JavaVirtualMachines
adoptopenjdk-8.jdk
openjdk-11.0.2.jdk
You can see the adoptopenjdk-8.jdk directory. (This directory changes depending on the user) It cannot be obtained from / usr / libexec / java_home, so add it with jenv add.
$ jenv add /Library/Java/JavaVirtualMachines/adoptopenjdk-8.jdk/Contents/Home/
openjdk64-1.8.0.222 added
1.8.0.222 added
1.8 added
$ jenv versions
* system
1.8
1.8.0.222
openjdk64-1.8.0.222
11.0
11.0.2
openjdk64-11.0.2
oracle64-1.8.0.202-ea
With reference to the following, I removed the redundant parts and added the missing parts.
GitHub - jenv/jenv: Manage your Java environment https://github.com/jenv/jenv
that's all.
Recommended Posts