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



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](https://github.com/Homebrew/homebrew-cask-versions)


[homebrew-caskって何？？？ - Qiita](https://qiita.com/yosshi4486/items/3aca4950bf0adcdf419d)


```
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](https://www.jenv.be/)



## 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
```