Java
Mac
brew
java8
Java10

MacのBrewでJava8 + Java10を利用する

Homebrewのインストール

homebrew-cask-versionsの導入

$ brew tap caskroom/versions

Java 10のインストール

  • caskでインストールされるJavaは最新版のJava SE 10になる。
$ brew cask install java

Java 8のインストール

$ brew cask install java8

インストール済みJDK(JAVA_HOME)の確認

  • /usr/libexec/java_homeコマンド
$ /usr/libexec/java_home -V
Matching Java Virtual Machines (2):
    10.0.2, x86_64: "Java SE 10.0.2"    /Library/Java/JavaVirtualMachines/jdk-10.0.2.jdk/Contents/Home
    1.8.0_181, x86_64:  "Java SE 8" /Library/Java/JavaVirtualMachines/jdk1.8.0_181.jdk/Contents/Home

/Library/Java/JavaVirtualMachines/jdk-10.0.2.jdk/Contents/Home
$ /usr/libexec/java_home
/Library/Java/JavaVirtualMachines/jdk-10.0.2.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の切り替え

Java SE 10

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

Java SE 8

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