遂にkotlinデビュー
先日、新しい案件を始めるにあたりサーバーサイドkotlinをやってみよう!という話になったため、Macのローカル環境にkotlin開発環境を構築してみたので、手順をご紹介しようと思います♪
Using Kotlin for Server-side Development Kotlin is a great fit for developing server-side applications, allowing you to write concise and expressive code while maintaining full compatibility with existing Java-based technology stacks and a smooth learning curve: Kotlin for Server Side - Kotlin Programming Language
KotlinはGoogleが開発したAndroid用アプリケーションの言語として開発されましたが、完全なJava言語のサポートがあるためサーバーサイドでも使われる事が多いですね。
SpringBootなどのメジャーなフレームワークも手厚くサポートしているため使いやすいのも助かります♪
手順
HomebrewでKotlinをインストール
まずはHomebrew経由でKotlinをインストールしましょう。
brew install kotlin
==> Downloading https://homebrew.bintray.com/bottles/openjdk-15.0.1.mojave.bottle.tar.gz
==> Downloading from https://d29vzk4ow07wi7.cloudfront.net/a4f00dc8b4c69bff53828f32c82b0a6be41b23a69a7775a95cdbc9e01d9bdb68?response-content-disposition=attachment%3Bfilename%3D%22openjdk-15.0.1.mojave.bottle.tar.gz%22&Policy=eyJTdGF0ZW1l
######################################################################## 100.0%
==> Downloading https://github.com/JetBrains/kotlin/releases/download/v1.4.21/kotlin-compiler-1.4.21.zip
==> Downloading from https://github-production-release-asset-2e65be.s3.amazonaws.com/3432266/0a09f880-388a-11eb-927b-08b36198f3e7?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIAIWNJYAX4CSVEH53A%2F20201218%2Fus-east-1%2Fs3%2Faws4_re
######################################################################## 100.0%
==> Installing dependencies for kotlin: openjdk
==> Installing kotlin dependency: openjdk
==> Pouring openjdk-15.0.1.mojave.bottle.tar.gz
==> Caveats
For the system Java wrappers to find this JDK, symlink it with
sudo ln -sfn /usr/local/opt/openjdk/libexec/openjdk.jdk /Library/Java/JavaVirtualMachines/openjdk.jdk
openjdk is keg-only, which means it was not symlinked into /usr/local,
because it shadows the macOS `java` wrapper.
If you need to have openjdk first in your PATH run:
echo 'set -g fish_user_paths "/usr/local/opt/openjdk/bin" $fish_user_paths' >> ~/.config/fish/config.fish
For compilers to find openjdk you may need to set:
set -gx CPPFLAGS "-I/usr/local/opt/openjdk/include"
==> Summary
🍺 /usr/local/Cellar/openjdk/15.0.1: 614 files, 323.8MB
==> Installing kotlin
🍺 /usr/local/Cellar/kotlin/1.4.21: 108 files, 64.5MB, built in 5 seconds
==> Upgrading 1 dependent:
sbt 1.3.8_1 -> 1.4.4
==> Upgrading sbt 1.3.8_1 -> 1.4.4
==> Downloading https://github.com/sbt/sbt/releases/download/v1.4.4/sbt-1.4.4.tgz
==> Downloading from https://github-production-release-asset-2e65be.s3.amazonaws.com/279553/e41a1b00-2d03-11eb-8ea6-86214345c60c?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIAIWNJYAX4CSVEH53A%2F20201218%2Fus-east-1%2Fs3%2Faws4_req
######################################################################## 100.0%
==> Caveats
You can use $SBT_OPTS to pass additional JVM options to sbt.
Project specific options should be placed in .sbtopts in the root of your project.
Global settings should be placed in /usr/local/etc/sbtopts
==> Summary
🍺 /usr/local/Cellar/sbt/1.4.4: 13 files, 48MB, built in 4 seconds
Removing: /usr/local/Cellar/sbt/1.3.8_1... (801 files, 61MB)
==> Checking for dependents of upgraded formulae...
==> No broken dependents found!
==> Caveats
==> openjdk
For the system Java wrappers to find this JDK, symlink it with
sudo ln -sfn /usr/local/opt/openjdk/libexec/openjdk.jdk /Library/Java/JavaVirtualMachines/openjdk.jdk
openjdk is keg-only, which means it was not symlinked into /usr/local,
because it shadows the macOS `java` wrapper.
If you need to have openjdk first in your PATH run:
echo 'set -g fish_user_paths "/usr/local/opt/openjdk/bin" $fish_user_paths' >> ~/.config/fish/config.fish
For compilers to find openjdk you may need to set:
set -gx CPPFLAGS "-I/usr/local/opt/openjdk/include"
==> sbt
You can use $SBT_OPTS to pass additional JVM options to sbt.
Project specific options should be placed in .sbtopts in the root of your project.
Global settings should be placed in /usr/local/etc/sbtopts
確認
以下のコマンドで確認しましょう。
Kotlin version 1.4.21-release-351 (JRE 15.0.1+9)
無事にインストールされましたね♪
HelloWorldを出力してみよう
では、次はプログラミングの定番、HelloWorldを出力してみましょう。
以下のファイルを用意して、コンパイル後に実行してみます。
fun main(args:Array<String>) {
println("Hello World!")
}
kotlinc helloWorld.kt -include-runtime -d helloWorld.jar
OpenJDK 64-Bit Server VM warning: Options -Xverify:none and -noverify were deprecated in JDK 13 and will likely be removed in a future release.
WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by com.intellij.util.ReflectionUtil to method java.util.ResourceBundle.setParent(java.util.ResourceBundle)
WARNING: Please consider reporting this to the maintainers of com.intellij.util.ReflectionUtil
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release
helloWorld.kt:1:10: warning: parameter 'args' is never used
fun main(args:Array<String>) {
kotlin helloWorld.jar
Hello World!
コンパイルと実行も無事に出来ましたね♪
終わりに
以上のようにインストールから実行まで確認出来ました。
次回はVSコードを使ってKotlinを記述するための設定を進めていこうと思います♪

Using Kotlin for Server-side Development
Kotlin is a great fit for developing server-side applications, allowing you to write concise and expressive code while maintaining full compatibility with existing Java-based technology stacks and a smooth learning curve: