First look at Kotlin - my first pains

Sep 12, 2018
•
johnny

Intro

I’ve been wanting to take a look at Kotlin for a while and have been delaying, but this past weekend I had a couple of hours I had no good idea how to use them so… I finally wrote some Kotlin code (granted the result is about 30 lines long and most of them I’ve seen on tutorials, but still, I typed it and saw it running on my computer!).

As I didn’t really do anything of much interest, this post is basically about a couple of problems I ran into while following some tutorials.

Getting started

I already had IntelliJ installed (for along time I might add) in hopes of playing around with Kotlin, so that step was out of the way. What I did need to install was the Java JDK.

With the required software setup done, I headed on to the Kotlin website to see how to start doing something. I ended up on the docs reference page and started reading through. After the basics I lost my patience and having noticed Ktor, for building web applications, in the docs, I decided to start building a web api.

It says it can’t find the entry point, which is on the Server.kt file, in the ktkindergarten package. It all seemed right to me, as I had seen in a couple of Kotlin examples, what was the problem?!

It took me hoooooooours but finally figured it out. As you can see, I’m not really declaring a class, only the main function on the Server.kt file. What Kotlin does is generate a class with the name of the file with a Kt suffix. So the fix for this issue that was driving me crazy was just to go to the build.gradle file and change the line mainClassName = 'ktkindergarten.Server' to mainClassName = 'ktkindergarten.ServerKt'.

(now you see why I’m posting this… so I can avoid future stupid problems of the sort 🤬)

The first error was bad, but I think one understands better that a class cannot be found (even for an obscure reason) than this. java.lang.VerifyError: Bad type on operand stack - what?!

Ok, the problem here was a version mismatch between the installed Kotlin version and the one I declared on the build.gradle file. So, changing the line ext.kotlin_version = '1.2.41' to ext.kotlin_version = '1.2.61' fixed it (at the time I was testing this).

Outro

If someone gets into the same problems I did, I hope Google helps them find this post (or any other search engine for that matter), it would have saved me some hours 😛