Homebrewでjavaをインストールする方法


java11をインストールする

  • 環境 : macOS Catalina バージョン10.15.7
% brew update
Updated 3 taps (homebrew/core, homebrew/cask and homebrew/cask-fonts).
==> New Formulae
# ...省略...

# バージョンを見てみる
% brew -v
Homebrew 2.5.6
Homebrew/homebrew-core (git revision 50dda; last commit 2020-10-23)
Homebrew/homebrew-cask (git revision fb5c8; last commit 2020-10-23)

# Homebrewが元気かお医者さんに聞いておく
% brew doctor
Your system is ready to brew.

# インストールしちゃってないか確認する
% /usr/libexec/java_home -V
Unable to find any JVMs matching version "(null)".
Matching Java Virtual Machines (0):

Default Java Virtual Machines (0):

No Java runtime present, try --request to install.

# java11を探す
% brew search java11
==> Formulae
java11

# インストールする
% brew install java11
Updating Homebrew...
==> Auto-updated Homebrew!
Updated 2 taps (homebrew/core and homebrew/cask).
==> Updated Formulae
Updated 1 formula.
==> Updated Casks
blueharvest                                       cctalk                                            chromium

==> Downloading https://homebrew.bintray.com/bottles/openjdk%4011-11.0.8.catalina.bottle.tar.gz
==> Downloading from https://d29vzk4ow07wi7.cloudfront.net/5cf17a69c7f88b8f721959391d89eafcf1dc6ebcbe5f1496b99448ac9fce0c1d?response-content-disposi
######################################################################## 100.0%
==> Pouring [email protected]
==> Caveats
For the system Java wrappers to find this JDK, symlink it with
  sudo ln -sfn /usr/local/opt/openjdk@11/libexec/openjdk.jdk /Library/Java/JavaVirtualMachines/openjdk-11.jdk

openjdk@11 is keg-only, which means it was not symlinked into /usr/local,
because this is an alternate version of another formula.

If you need to have openjdk@11 first in your PATH run:
  echo 'export PATH="/usr/local/opt/openjdk@11/bin:$PATH"' >> ~/.zshrc

For compilers to find openjdk@11 you may need to set:
  export CPPFLAGS="-I/usr/local/opt/openjdk@11/include"

==> Summary
🍺  /usr/local/Cellar/openjdk@11/11.0.8: 650 files, 295.3MB
==> `brew cleanup` has not been run in 30 days, running now...
Removing: /Users/ponsuke/Library/Caches/Homebrew/git--2.28.0.catalina.bottle.tar.gz... (19.4MB)
Removing: /Users/ponsuke/Library/Caches/Homebrew/Cask/pycharm-ce--2020.2.2,202.7319.64.dmg... (381.6MB)

# インストールのメッセージにある通り環境変数を設定する
% echo 'export PATH="/usr/local/opt/openjdk@11/bin:$PATH"' >> ~/.zshrc
% export CPPFLAGS="-I/usr/local/opt/openjdk@11/include"
% source ~/.zshrc

# バージョンを確認する
% java -version
openjdk version "11.0.8" 2020-07-14
OpenJDK Runtime Environment (build 11.0.8+11)
OpenJDK 64-Bit Server VM (build 11.0.8+11, mixed mode)

java6をインストールする

(しなくてもいいけど)事前準備をする

# アップデートしておく
$ brew update
Updated 2 taps (homebrew/core and homebrew/cask).
==> New Formulae
# <省略>
$ brew -v
Homebrew 2.1.4
Homebrew/homebrew-core (git revision d2f75; last commit 2019-06-03)
Homebrew/homebrew-cask (git revision db1f8; last commit 2019-06-03)

# Homebrewが元気かお医者さんに聞いておく
$ brew doctor
Your system is ready to brew.

# インストールしちゃってないか確認する
$ /usr/libexec/java_home -V
Matching Java Virtual Machines (3):
    1.8.0_131, x86_64:  "Java SE 8" /Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home
    1.8.0_45, x86_64:   "Java SE 8" /Library/Java/JavaVirtualMachines/jdk1.8.0_45.jdk/Contents/Home
    1.7.0_67, x86_64:   "Java SE 7" /Library/Java/JavaVirtualMachines/jdk1.7.0_67.jdk/Contents/Home

cask-versionsをbrew tapする

古いバージョンをインストールするためにhomebrew-cask-versionstapする

# cask-versionsをtapする
$ brew tap homebrew/cask-versions
Updating Homebrew...
==> Auto-updated Homebrew!
Updated 2 taps (homebrew/core and homebrew/cask).
No changes to formulae.

==> Tapping homebrew/cask-versions
Cloning into '/usr/local/Homebrew/Library/Taps/homebrew/homebrew-cask-versions'...
remote: Enumerating objects: 198, done.
remote: Counting objects: 100% (198/198), done.
remote: Compressing objects: 100% (194/194), done.
remote: Total 198 (delta 9), reused 28 (delta 1), pack-reused 0
Receiving objects: 100% (198/198), 84.74 KiB | 366.00 KiB/s, done.
Resolving deltas: 100% (9/9), done.
Tapped 169 casks (216 files, 324.9KB).

# tapできたか確認する
$ brew tap
homebrew/cask
homebrew/cask-versions
homebrew/core

# java6を探す
$ brew search java6
==> Casks
java6

インストールする

Mac に brew で Oracle Java のバージョンを指定してインストールする方法 - Qiita

# インストールする
$ brew cask install java6
==> Satisfying dependencies
==> Downloading https://support.apple.com/downloads/DL1572/en_US/javaforosx.dmg
==> Downloading from https://updates.cdn-apple.com/2018/macos/031-33898-20171026-7a797e9e-b8de-11e7-b1fe-c14fbda7e146/javaforosx.dmg
######################################################################## 100.0%
==> Verifying SHA-256 checksum for Cask 'java6'.
==> Installing Cask java6
==> Running installer for java6; your password may be necessary.
==> Package installers may write to any location; options such as --appdir are ignored.
Password:
installer: Package name is Java for macOS 2017-001
installer: Installing at base path /
installer: The install was successful.
🍺  java6 was successfully installed!

# インストールされているjavaを確認する
$ /usr/libexec/java_home -V
Matching Java Virtual Machines (5):
    1.8.0_131, x86_64:  "Java SE 8" /Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home
    1.8.0_45, x86_64:   "Java SE 8" /Library/Java/JavaVirtualMachines/jdk1.8.0_45.jdk/Contents/Home
    1.7.0_67, x86_64:   "Java SE 7" /Library/Java/JavaVirtualMachines/jdk1.7.0_67.jdk/Contents/Home
    1.6.0_65-b14-468, x86_64:   "Java SE 6" /Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home
    1.6.0_65-b14-468, i386: "Java SE 6" /Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home

/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home

MacでインストールされているJavaを確認する方法 - Qiita

(必要があれば)使うjavaのバージョンを切り替える

MacでJavaのバージョンを切り替える方法 - Qiita