Using multiple versions of Java with Brew on Mac + jEnv

Homebrew installation

--See Homebrew site - https://brew.sh

Introduction of homebrew-cask-versions

$ brew tap homebrew/cask-versions

Java 14 installation

--Java installed by cask install java will be the latest version (currently Java SE 14).

$ brew cask install java

Java 11 installation

$ brew cask install java11

Java 8 installation

--__ Not available due to license. AdoptOpenJDK, Corretto, [Zulu](https://jp.azul.com/downloads/ Substitute with zulu /) etc. __ -The SDK included differs depending on the JDK, so be careful.

$ brew cask install java8

Install AdoptOpenJDK 8

$ brew cask install adoptopenjdk8

Installing Amazon Corretto 8

$ brew cask install corretto8

Install Zulu 8

$ brew cask install zulu8

Check the installed JDK (JAVA_HOME)

--/ usr / libexec / java_home command

$ /usr/libexec/java_home -V
Matching Java Virtual Machines (3):
    12.0.1, x86_64:	"OpenJDK 12.0.1"	/Library/Java/JavaVirtualMachines/openjdk-12.0.1.jdk/Contents/Home
    11.0.2, x86_64:	"OpenJDK 11.0.2"	/Library/Java/JavaVirtualMachines/openjdk-11.0.2.jdk/Contents/Home
    1.8.0_212, x86_64:	"AdoptOpenJDK 8"	/Library/Java/JavaVirtualMachines/adoptopenjdk-8.jdk/Contents/Home

/Library/Java/JavaVirtualMachines/openjdk-12.0.1.jdk/Contents/Home
$ /usr/libexec/java_home --help
Usage: java_home [options...]
    Returns the path to a Java home directory from the current user's settings.

Options:
    [-v/--version   <version>]       Filter Java versions in the "JVMVersion" form 1.X(+ or *).
    [-a/--arch      <architecture>]  Filter JVMs matching architecture (i386, x86_64, etc).
    [-d/--datamodel <datamodel>]     Filter JVMs capable of -d32 or -d64
    [-t/--task      <task>]          Use the JVM list for a specific task (Applets, WebStart, BundledApp, JNI, or CommandLine)
    [-F/--failfast]                  Fail when filters return no JVMs, do not continue with default.
    [   --exec      <command> ...]   Execute the $JAVA_HOME/bin/<command> with the remaining arguments.
    [-R/--request]                   Request installation of a Java Runtime if not installed.
    [-X/--xml]                       Print full JVM list and additional data as XML plist.
    [-V/--verbose]                   Print full JVM list with architectures.
    [-h/--help]                      This usage information.

JDK switching

--Switch Java version on OS X --Set the environment variable JAVA_HOME using the java_home command. --Add to the .bashrc and .zshrc files according to the shell you are using.

Java SE 14

$ export JAVA_HOME=`/usr/libexec/java_home -v "14"`
$ PATH=${JAVA_HOME}/bin:${PATH}

Java SE 11

$ export JAVA_HOME=`/usr/libexec/java_home -v "11"`
$ PATH=${JAVA_HOME}/bin:${PATH}

Java SE 8

$ export JAVA_HOME=`/usr/libexec/java_home -v "1.8"`
$ PATH=${JAVA_HOME}/bin:${PATH}

Use of jEnv

--By introducing jEnv, it becomes easy to switch Java versions. --Reference: https://github.com/jenv/jenv

Install jEnv

--Install with Homebrew

$ brew install jenv

--Set PATH --Note that it depends on the shell used.

# Shell: bash
$ echo 'export PATH="$HOME/.jenv/bin:$PATH"' >> ~/.bash_profile
$ echo 'eval "$(jenv init -)"' >> ~/.bash_profile
# Shell: zsh
$ echo 'export PATH="$HOME/.jenv/bin:$PATH"' >> ~/.zshrc
$ echo 'eval "$(jenv init -)"' >> ~/.zshrc

--Create a directory

$ cd ~
$ mkdir ~/.jenv/versions

Add JDK to jEnv

--Install the JDK with Homebrew Cask. --Add the installed JDK to jEnv.

Addition of Java 8


$ jenv add `/usr/libexec/java_home -v "1.8"`

List of JDKs added to jEnv

--The JDK with * at the beginning is enabled.

$ jenv versions
* system (set by /Users/[User name]/.jenv/version)
  1.8
  1.8.0.222
  openjdk64-1.8.0.222

Java switching

--You can switch to any version output by jenv versions.

global

$ jenv global 1.8.0.222
$ jenv versions
  system
  1.8
* 1.8.0.222 (set by /Users/[User name]/.jenv/version)
  openjdk64-1.8.0.222
$ java -version
openjdk version "1.8.0_222"
OpenJDK Runtime Environment (AdoptOpenJDK)(build 1.8.0_222-b10)
OpenJDK 64-Bit Server VM (AdoptOpenJDK)(build 25.222-b10, mixed mode)

local

--Applies only to specific directories. --A .java-version file will be created in the directory where you executed the command, and the specified version of the JDK will be available.

$ jenv local 11.0
$ jenv versions                                                                                           ✘ 
  system
  1.8
  1.8.0.222
* 11.0 (set by /Users/[Directory where jenv local was executed]/.java-version)
  11.0.2
  openjdk64-1.8.0.222
  openjdk64-11.0.2
$ java -version
openjdk version "11.0.2" 2019-01-15
OpenJDK Runtime Environment 18.9 (build 11.0.2+9)
OpenJDK 64-Bit Server VM 18.9 (build 11.0.2+9, mixed mode)
$ cat .java-version
11.0

Automatic setting of environment variable JAVA_HOME

--To set JAVA_HOME automatically, execute ʻenable-plugin export. --Execute disable-plugin export` to cancel the automatic setting.

$ jenv enable-plugin export

Recommended Posts

Using multiple versions of Java with Brew on Mac + jEnv
Switch between multiple versions of Java on Mac
Use jenv to enable multiple versions of Java
Create a Java development environment using jenv on Mac
Install java and maven using brew on new mac
Manage multiple versions of Ruby on Mac [macOS + rbenv]
Using JupyterLab + Java with WSL on Windows 10
How to switch Java versions on Mac
Java version control with jenv on OSX
Install java and android-sdk on Mac using homebrew
Easily switch Java versions with alias on macOS
Install Java on Mac
Using Java 8 with Bluemix (on Liberty Runtime & DevOps Service)
Build Java development environment with VS Code on Mac
As of April 2018 How to get Java 8 on Mac
[Java] Development with multiple files using package and import
Java version control with jenv
Using Mapper with Java (Spring)
Sample of using Salesforce's Bulk API from Java client with PK-chunking
I just want to write Java using Eclipse on my Mac
How to switch Java version with direnv in terminal on Mac
Is the version of Elasticsearch you are using compatible with Java 11?
Using Java on OSX 10.15 (Catalina) β
Install Java with zip on Windows
Installed on Lombok Mac (using STS)
Try using Redis with Java (jar)
Handling of time zones using Java
Java build with mac vs code
Using Java with AWS Lambda-Eclipse Preparation
Html5 development with Java using TeaVM
Summary of object-oriented programming using Java
Using proxy service with Java crawling
Install Java development environment on Mac
I tried using GoogleHttpClient of Java
Steps to run local development of ASP.NET Core on Mac with docker-compose
How to check Java installed on Mac
Using Java with AWS Lambda-Implementation-Check CloudWatch Arguments
Java development environment construction memo on Mac
Sort by multiple conditions using Java Stream
Notes on operators using Java ~ String type ~
Sobel filter using OpenCV on Android (Java)
Notes on using FCM with Ruby on Rails
Build a Java development environment on Mac
Install Java8 with Yum on Amazon Linux
Game development with two people using java 2
I tried using OpenCV with Java + Tomcat
Game development with two people using java 1
Hello World on Mac VS Code Java
Status monitoring of java application using Elasticsearch
Looking back on the basics of Java
Using SSIServlet with built-in Tomcat of SpringBoot 2.2.4
Memory measurement of Java application on Windows
Acquisition of input contents using Scanner (Java)
Build OpenCV with Java Wrapper on Ubuntu 18.04
Java version control using cask + anyenv + jenv
Uncertainty of common Jar for multiple versions
Open multiple workspaces in Eclipse on Mac
Game development with two people using java 3
How to switch between multiple Java versions
Run Ubuntu + ROS with Docker on Mac
Try using the Wii remote with Java