sdkman + kotlin

Soon I’ll need to add another language to my stack — Kotlin. The process of getting comfortable with the syntax doesn’t worry me; that’s the easy part. As always, what I care about most is making the setup simple, fast, and reproducible — especially the local developer environment. I won’t be the only person on the team, and it matters that my coworkers’ compiler versions line up with mine down to the patch.

Installing Kotlin on a local machine requires installing the JDK. Plenty of people will say: “Well, just go download it, what’s the problem?” Same problem as always — an identical environment for every team member.

Just like asdf, the java world has its own well-established JDK manager — sdkman. It lets you:

Installing sdkman itself is documented on the official site. Here’s how to install the JDK and Kotlin:

sdk install java 20.0.1-oracle
sdk install kotlin # latest version

A few words about compiling by hand. Obviously on larger projects you always have a build system — there’s no avoiding it. But if you’re just getting acquainted with the language, you want a result faster, without spinning up a project.

Say you’ve created a hello.kt file:

fun main(args: Array<String>) {
  var hello = "Hello, Kotlin!"
  println(hello)
}

And you want to compile it into a jar:

kotlinc hello.kt -d hello.jar

When you run it, you’ll most likely hit the following error:

Exception in thread "main" java.lang.NoClassDefFoundError: kotlin/jvm/internal/Intrinsics
        at HelloKt.main(hello.kt)
Caused by: java.lang.ClassNotFoundException: kotlin.jvm.internal.Intrinsics
        at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:641)
        at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:188)
        at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
        ... 1 more

To avoid it, you need to include the runtime in your jar:

kotlinc hello.kt -include-runtime -d hello.jar

Now your file will run without issues:

java -jar hello.jar
Hello, Kotlin!

Good luck picking up Kotlin — and install the software you need the Unix way.

References