[Java] Java version control using cask+anyenv+jenv

2 minute read


OS: macOS Catalina 10.15.5 brew: 2.4.3

Install Java using cask

If you use brew cask as it is, you cannot install an old version of Java. So use the following command to install an older version.

$ brew tap homebrew/cask-versions

Install various versions

// Check the installable version of java
$ brew search java

// java14
$ brew cask install java

// java11
$ brew cask install java11

// java8
$ brew cask install adoptopenjdk8

// java version check
$ /usr/libexec/java_home -V

Install #anyenv anyenv can manage *env. In this case, it is introduced to manage jenv.

// install anyenv with brew
$ brew install anyenv

// Add path zsh version
$ echo'eval "$(anyenv init -)"' >> ~/.zshrc

// Create manifest directory
$ anyenv install --init
This will create the .anyenv directory.

// restart SHELL
$ exec $SHELL -l

// anyenv command
// Check the version of installed *env
$ anyenv versions

// list of *env that can be installed with anyenv
$ anyenv install -l
  • Good thing If the setting of zshrc is not reflected, reflect it with source ~/.zshrc

Include a convenient anyenv login

Plug-ins are optional, but it is convenient to include them.

// create a directory for the plugin
$ mkdir -p ~/.anyenv/plugins

// anyenv-update Plugin that updates *env entered in anyenv in a batch
$ git clone https://github.com/znz/anyenv-update.git ~/.anyenv/plugins/anyenv-update

// anyenv-Plugin that can execute *env entered with git anyenv with git command
$ git clone https://github.com/znz/anyenv-git.git ~/.anyenv/plugins/anyenv-git

install jenv

Manage Java installed by cask

// install jenv
$ anyenv install jenv

// restart SHELL
$ exec $SHELL -l

// Confirmation
$ anyenv versions
* 14.0.1 (set by /Users/masa/.anyenv/envs/jenv/version)

Setting to manage Java with jenv

It is a method of registering various Java versions in jenv and switching.

// add to the associated jenv
$ jenv add `/usr/libexec/java_home -v "1.8"`
$ jenv add `/usr/libexec/java_home -v "11"`
$ jenv add `/usr/libexec/java_home -v "14"`

// Check the version of java managed by jenv
$ jenv versions

// global setting
$ jenv global 11.0.2

// java version check
$ java versions

// start shell
$ jenv exec zsh

// Set JAVA_HOME automatically
$ jenv enable-plugin export

// Confirm JAVA_HOME
$ echo $JAVA_HOME

// Topic When using another version of JDK in a specific directory
$ jenv local 11.0.2

*If the JAVA_HOME setting is not reflected properly, it may be reflected by restarting terminal.