[JAVA] Install and configure jenv on macOS

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

Premise

environment

Confirmed with macOS Mojave 10.14.6

Create ~ / .jenv

mkdir ~/.jenv

Install jenv

1. When using homebrew

brew install jenv

2. When cloning from git

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

Rebooting the shell

manner

Operation check of jenv

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

Check JAVA_HOME settings

echo ${JAVA_HOME}
jenv enable-plugin export
exec $SHELL -l

Java environment installation

When installing Java environment with brew cask

brew cask install java

For Java 8

brew cask install java8

Added Java environment

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

View Java environment version list


$ 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.

Java environment selection

$ jenv local 11.0.2
$ exec $SHELL -l
$ cat .java-version
11.0.2

Check if it is set

echo ${JAVA_HOME}
/Users/hogehoge/.jenv/versions/11.0.2

Setting is OK with this.

Delete .java -version because you don't need it

rm .java-version

Global Java version settings

Required only when you want to set it globally

jenv global 11.0.2

Shell Java version setting

jenv shell 11.0.2

Common workflow

Use two JVMs on macOS

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

reference

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

Install and configure jenv on macOS
Install OpenJDK on macOS
Install and configure Keycloak
Install Rails on macOS Catalina
Install JDK and JRE on Ubuntu 16.10
Install Java 9 on windows 10 and CentOS 7
How to install and configure the monitoring tool "Graphite" on Ubuntu
Install docker and docker-compose on Alpine Linux
Install imagick on macOS Catalina + php 7.4 Note
Install java and android-sdk on Mac using homebrew
Install Docker on Raspberry Pi 4 and Raspberry Pi OS 64bit
Install Java 11 (OpenJDK: AdoptOpenJDK) on macOS with Homebrew
Install Java 14 (OpenJDK: AdoptOpenJDK) on macOS with Homebrew
Install SonarQube on Mac and challenge source analysis
Install Java 8 (OpenJDK: AdoptOpenJDK) on macOS with Homebrew
Install and switch between multiple Javas on Ubuntu
Introducing Eclipse IoT-Part2: Install Eclipse Neon.2 on macOS Sierra
Install gradle on mac
Install Corretto 8 on Windows
Install Java on Mac
Install Golang on CentOS 8
Install pyqt5 on ubuntu
Install Neo4j 4.1.3 on centOS
Install Java 8 (OpenJDK: Amazon Corretto) on macOS with Homebrew
Install Docker on Manjaro
Install Vertica 10.0 on CentOS 6.10
Install Ruby on Ubuntu 20.04
Install java and maven using brew on new mac
Install PostgreSQL 12 on Centos8
Install nginx on centOS7
Install lombok on SpringToolSuite4
Install Python 3 on CentOS 7
Install kuromoji on CentOS7
Install Eclipse on Mac and translate it into Japanese
Install Java 8 (OpenJDK: Zulu Community) on macOS with Homebrew
Install Autoware on Ubuntu 18.04.5
Install Ruby 3.0.0-preview1 on macOS (also without Homebrew ruby-build)
Install Mattermost on CentOS 7
Install PostGIS 2.5.5 on CentOS7
Install rbenv with apt on ubuntu and put ruby
Install jpndistrict on CentOS 7
Install openjdk11 on mac
Install Homebrew on Ubuntu 20.04
Install buildah and container-selinux
Install Redmine 4.1.1 on CentOS 7
Install OpenJDK 8 on mac
Smokeping Install on CentOS7
Install PostgreSQL 13 on CentOS 7.5
Install docker and docker-compose on ubuntu in the shortest process
How to Install Elixir and Phoenix Framework on Ubuntu 20.04 LTS
Install rsync on CentOS8 (version check) and file backup (synchronization)
[Java development environment construction] Install OpenJDK 11 (Java 11) on macOS with Homebrew.
Java version control on macOS
Install Docker on Raspberry Pi
Install Docker on Windows 10 PRO
Setup of JMeter and jEnv
Install OpenJDK7 (JAVA) on ubuntu 14.04
Install Cybozu Office 10 on Ubuntu 20.4
Install Docker on Ubuntu Server 20.04
Install rbenv on Amazon Linux
Install zabbix agent (5.0) on Ubuntu 18.04