5
9

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Mac > jEnvで複数バージョンのJDK(JAVA環境)を切り替える

Last updated at Posted at 2020-10-06

2020/12/25 変更

※ 変更1

Error: Calling brew cask install is disabled! Use brew install [--cask] instead.

とエラーが出てたので、

brew cask install
 ↓
brew install cask

と変更しました

※ 変更2
corretto -> corretto11

install java

homebrew-cask-versions

Homebrew/homebrew-cask-versions: 🔢 Alternate versions of Casks

homebrew-caskって何??? - Qiita

brew tap homebrew/cask-versions

install

You can now install alternate versions of Casks.

JDK 21

corretto 21

corretto21
brew install cask corretto21

JDK 17

corretto 17

corretto17
brew install cask corretto17

JDK 11

corretto 11

corretto11
brew install cask corretto11

AdoptOpenJDK 11

adoptopenjdk11
brew tap AdoptOpenJDK/openjdk
brew install cask adoptopenjdk11

JDK 8

corretto 8

corretto8
brew install cask corretto8

AdoptOpenJDK 8

adoptopenjdk8
brew install cask adoptopenjdk8

確認

/usr/libexec/java_home -V

result

result
Matching Java Virtual Machines (3):
    11.0.8, x86_64:	"Amazon Corretto 11"	/Library/Java/JavaVirtualMachines/amazon-corretto-11.jdk/Contents/Home
    1.8.0_265, x86_64:	"Amazon Corretto 8"	/Library/Java/JavaVirtualMachines/amazon-corretto-8.jdk/Contents/Home
    1.8.0_265, x86_64:	"AdoptOpenJDK 8"	/Library/Java/JavaVirtualMachines/adoptopenjdk-8.jdk/Contents/Home

jenvのインストール

jEnv - Manage your Java environment

brewでインストール

brew install jenv

設定

Bashを使っている場合

Bash
echo 'export PATH="$HOME/.jenv/bin:$PATH"' >> ~/.bash_profile
echo 'eval "$(jenv init -)"' >> ~/.bash_profile

Zshを使っている場合

Zsh
echo 'export PATH="$HOME/.jenv/bin:$PATH"' >> ~/.zshrc
echo 'eval "$(jenv init -)"' >> ~/.zshrc

ディレクトリを作成

mkdir ~/.jenv
mkdir ~/.jenv/versions

.bash_profile を再読み込み

source ~/.bash_profile

jEnvにJDKを追加 (Configure)

JDK 21

corretto 21

jenv add /Library/Java/JavaVirtualMachines/amazon-corretto-21.jdk/Contents/Home

JDK 17

corretto 17

corretto11
jenv add /Library/Java/JavaVirtualMachines/amazon-corretto-17.jdk/Contents/Home

JDK 11

corretto 11

corretto11
jenv add /Library/Java/JavaVirtualMachines/amazon-corretto-17.jdk/Contents/Home
result
result
corretto64-11.0.8 added
11.0.8 added
11.0 added
11 added

AdoptOpenJDK 11

adoptopenjdk11
jenv add /Library/Java/JavaVirtualMachines/adoptopenjdk-11.jdk/Contents/Home
result
result
openjdk64-11.0.8 added
11.0.8 added
 11.0 already present, skip installation
 11 already present, skip installation

JDK 8

corretto 8

corretto8
jenv add /Library/Java/JavaVirtualMachines/amazon-corretto-8.jdk/Contents/Home
result
result
corretto64-1.8.0.265 added
1.8.0.265 added
1.8 added

AdoptOpenJDK 8

AdoptOpenJDK8
jenv add /Library/Java/JavaVirtualMachines/adoptopenjdk-8.jdk/Contents/Home
result
result
openjdk64-1.8.0.265 added
 1.8.0.265 already present, skip installation
 1.8 already present, skip installation

結果確認

jEnvに追加されているJDKの一覧確認

jenv versions

result

result
* system (set by /Users/atsu/.jenv/version)
  1.8
  1.8.0.265
  11
  11.0
  11.0.8
  corretto64-1.8.0.265
  corretto64-11.0.8
  openjdk64-1.8.0.265
  openjdk64-11.0.8

Javaの切り替え

jenv versionsで出力される任意のバージョンへ切り替える

Java環境の切替(グローバル)

corretto21へ変更

jenv global corretto64-21.0.2

corretto11へ変更

jenv global corretto64-11.0.8

adoptopenjdk11へ変更

jenv global openjdk64-11.0.8

corretto8へ変更

jenv global corretto64-1.8.0.265

adoptopenjdk8へ変更

jenv global openjdk64-1.8.0.265

結果確認

jenv versions

result

result
  system
  1.8
  1.8.0.265
  11
  11.0
  11.0.8
  corretto64-1.8.0.265
* corretto64-11.0.8 (set by /Users/atsu/.jenv/version)

Java環境の切替(ローカルディレクトリ)

特定のディレクトリのみに適用する

corretto8へ変更

jenv local corretto64-1.8.0.265

adoptopenjdk8へ変更

jenv local openjdk64-1.8.0.265

環境変数JAVA_HOMEの自動設定

jenvでのバージョン切り替えに応じて、自動的にJAVA_HOMEを設定するにはenable-plugin exportを実行する。

自動設定を有効

jenv enable-plugin export

result

result
You may restart your session to activate jenv export plugin echo export plugin activated

自動設定を解除

jenv disable-plugin export
5
9
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
9

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?