DEV Community

kaede
kaede

Posted on

kotlin 基礎 Part 10 -- Mac で openJDK と JAVA_HOME を動かす

why

Kotlin を Springboot で動かすには
openJDK が必須だから。
asdf を用いた手順を書いておく。


言語管理ツール asdf をインストール


asdf を DL

https://asdf-vm.com/guide/getting-started.html#_1-install-dependencies

git clone https://github.com/asdf-vm/asdf.git ~/.asdf --branch v0.11.1
Enter fullscreen mode Exit fullscreen mode

git clone でダウンロードできる。


asdf の読み込みを有効化

. "$HOME/.asdf/asdf.sh"
Enter fullscreen mode Exit fullscreen mode

.zshrc にこれを置くことで、起動時に asdf が有効化されると解釈。
Completions の設定は書かない。今回は zsh plugin 入れないので。

asdf list
No plugins installed
Enter fullscreen mode Exit fullscreen mode

シェルを再起動すると、無事に asdf が動かせた。



Java の Open JDK 17 をインストール


asdf に java プラグインを追加

asdf plugin-add java

asdf list
java
  No versions installed
Enter fullscreen mode Exit fullscreen mode

java のプラグインを追加
まだどのバージョンのも入れていない。


asdf の Open JDK リストを取得

asdf list-all java | grep openjdk-1 

...

openjdk-10
...
openjdk-19.0.2

Enter fullscreen mode Exit fullscreen mode

openjdk は 10 ~ 19.0.2 がインストールできる。


Open JDK 17.0.2 をインストール

asdf install java openjdk-17.0.2
...
openjdk-17.0.2_macos-x64_bin.tar.gz: OK
Enter fullscreen mode Exit fullscreen mode

以前開発で使っていた 17.0.2 をインストールする。

asdf list
java
  openjdk-17.0.2
Enter fullscreen mode Exit fullscreen mode

openjdk-17.0.2 が無事にインストールされた。


java コマンドを実行できるようにする

java

No version is set for command java                                                       
Consider adding one of the following versions in your config file at 
java openjdk-17.0.2
Enter fullscreen mode Exit fullscreen mode

現段階で java コマンドを実行する。
するとバージョン指定がされていないと出る。

asdf global java openjdk-17.0.2
Enter fullscreen mode Exit fullscreen mode

なので、グローバルに java 実行した時に
動くバージョンとして、 openjdk-17.0.2 を指定する。

java --version
openjdk 17.0.2 2022-01-18
OpenJDK Runtime Environment (build 17.0.2+8-86)
OpenJDK 64-Bit Server VM (build 17.0.2+8-86, mixed mode, sharing)

asdf which java
/Users/kaede0902/.asdf/installs/java/openjdk-17.0.2/bin/java
Enter fullscreen mode Exit fullscreen mode

これで java が使えるようになった。
また、java のバイナリの場所も見れるようになった。



JAVA_HOME を通す

echo $JAVA_HOME
Enter fullscreen mode Exit fullscreen mode

前述のように、Open JDK を入れただけでは
JAVA_HOME は通らない。

https://github.com/halcyon/asdf-java

この公式を参考に

. ~/.asdf/plugins/java/set-java-home.zsh
Enter fullscreen mode Exit fullscreen mode

これを zshrc に入れてシェルを再起動する。

$JAVA_HOME
/Users/kaede0902/.asdf/installs/java/openjdk-17.0.2
Enter fullscreen mode Exit fullscreen mode

これで、JAVA_HOME も通った。
JAVA_HOME が通ることにより、InteilliJ も動かせるようになる。

Top comments (0)

Great read:

Is it Time to go Back to the Monolith?

History repeats itself. Everything old is new again and I’ve been around long enough to see ideas discarded, rediscovered and return triumphantly to overtake the fad. In recent years SQL has made a tremendous comeback from the dead. We love relational databases all over again. I think the Monolith will have its space odyssey moment again. Microservices and serverless are trends pushed by the cloud vendors, designed to sell us more cloud computing resources.

Microservices make very little sense financially for most use cases. Yes, they can ramp down. But when they scale up, they pay the costs in dividends. The increased observability costs alone line the pockets of the “big cloud” vendors.