By now you should’ve realised I like to jump around a lot of languages. I’ll be looking at Kotlin in the holidays and thought I’d write some stuff as I go along. I’m most likely never going to cover installing and setting up a work environment - let’s face it, that’s the easy part.

Diving right in

.kt is the file extension used for your Kotlin files. Hope your text editor or IDE has a nice file tree icon.

First Step - Creating our file

Create a file and dump the following in there:

fun main(args: Array<String>) {
    println("Hello, World!")
}

Your stock standard “hello world” example. It’s time to fire up terminal or command line prompt if I remember my Windows days correctly.

Compiling

cd into the folder containing your Kotlin code. Next we’ll run the following command:

$ kotlinc HelloWorld.kt -include-runtime -d hello_world.jar

So what’s happening here. We’re providing kotlinc (compiler) the file that needs to be processed, then we send the -include-runtime option. This includes the Kotlin runtime. This allows the resulting jar file to be run. The -d option lets us provide the output path. This could be a directory of class files or just a plain jar file.

When you want to run your jar file, you’d call the following in the terminal:

$ java -jar hello.jar

That’s about it. If you want to take a deep dive into what’s possible, $ kotlinc -help is your friend.

Caveat

At this point in time when you run the compile command, you’ll get the following output:

WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by com.intellij.util.text.StringFactory to constructor java.lang.String(char[],boolean)
WARNING: Please consider reporting this to the maintainers of com.intellij.util.text.StringFactory
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

I haven’t quite figured out what the cause of this is, but I’m assuming it’s because I’m running Java 9. I’ll post a fix for this if I find it or if it’s necessary.