Trying Kotlin

Kotlin, the new programming language from JetBrain suddenly become overnight sensation after Google announcement at IO conference last week, that Kotlin will be officially supported for Android dev. Everyone people talking about how the new language will be the future … sigh.

Didn’t want to miss, I decided to look into this new language. I’m not a mobile dev so I try to look if Kotlin also possible for web dev. Turn out there’s a specific section in Kotlin docs for Web Development. There’s 2 tutorials and one is titled Creating Web Applications with Http Servlets. Let’s try it.

The first step of course installing all the Kotlin

Exception in thread "main" java.lang.UnsupportedClassVersionError: org/jetbrains/kotlin/preloading/Preloader : Unsupported major.minor version 52.0
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(
    at Method)
    at java.lang.ClassLoader.loadClass(
    at sun.misc.Launcher$AppClassLoader.loadClass(
    at java.lang.ClassLoader.loadClass(
    at sun.launcher.LauncherHelper.checkAndLoadMain(

Not really a good start. Next I try installing using homebrew:-

brew install kotlin

Also got the same error. Searching for the error message, I bumped into this bug report on Archlinux website. So indeed I’m having Java 7. So I try to install Java 8:-

brew cask install java

Now kotlin work !

The tutorial on the Kotlin docs only stop at gradle war to build the web app. Then proceeded with example of running that using IntelliJ IDEA. I have no plan to install that mighty IDE yet. There must be a way to run the war from command line, right ? Fortunately, I found a tutorial on using gretty. The changes pretty straigtforward as you can see in the screenshot below:-

The first try didn’t work though. Got this error:-

FAILURE: Build failed with an exception.

* Where:
Build file '/Users/kamal/kotlinc/src/kotlin-examples/tutorials/servlet-web-applications/build.gradle' line: 39

* What went wrong:
A problem occurred evaluating root project 'servlet-web-applications'.
> Could not set unknown property 'port' for object of type org.akhikhl.gretty.GrettyExtension.

Checking Gretty’s github, I noticed some notes on release 1.2.4:-

Properties servicePort and statusPort are deprecated and not used anymore. Gretty automatically selects available TCP ports for communication with it’s runner process.

Hmm, could be related. So I try to remove the port definition from build.gradle and run gradle war again. It’s compiled !

Then the next start just running gradle appStart and I got my “Hello world” running. The whole source (with gretty addition) on my Github.

Written on May 25, 2017