Looking into Kotlin

Kotlin is one of several languages built on top of the Java JVM. I recall, a long time back (when Microsoft had a Java offering) going to a conference on Java where everyone spoke about the Java language (it’s syntax etc.) being the key thing. However one speaker, from IBM, talked about the byte code being the most important part of Java. At the time he suggested how lots of different languages would become cross-platform using the Java byte code as their compiled unit. We ofcourse see this happening more and more now (and the same ofcourse can be said for .NET and the CIL).

In this post I’m looking at one of those languages (Kotlin) that uses the JVM and can compile to byte code.

Hello World

I know it’s tedious but we’re going to write the Hello World application in Kotlin to get everything up and running.

Whilst Kotlin can be compiled using Eclipse, I have IntelliJ on my machine so will be sticking with that for all development, hence instructions around using the development environment will be specific to IntelliJ.

Let’s create our first project (from IntelliJ)

  • File | New project, select Java then locate Kotlin/JVM in the additional libraries and frameworks tree
  • Tick the check box against Kotlin/JVM) and then click Next
  • Give your project a name, HelloWorld for example
  • Click the Finish button

This created a project and not much else of interest. So now…

  • Right mouse click on the src folder in the project view
  • Select New | Kotlin File/Class
  • Name it, for example HelloWorld
  • Click the OK button

Now we have a blank Kotlin file which defaulted to the .kt extension.

Simply type (or copy and paste) the following

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

Now, either create a configuration to run the code or more easily, right mouse click on the main function and select Run ‘HelloWorldKt’ which will create the configuration. If you choose to create the configuration yourself, then Main class: should be HelloWorldKt.

Note: HelloWorldKt was created by IntelliJ to wrap around the main function in a class, so that the JVM gets the correct method signature etc. So basically take the name of your file with the main method in and append Kt to it (and remove the extension).

IntelliJ (in this instance) created a folder off of our project named

out\production\<your_project_name>

Within this is the .class that your Kotlin code was compiled to.

It’s Java bytecode so we can…

Ultimately Kotlin turns source code into Java byte code, so we can easily create a JAR (for example) and execute our code. Let’s create a JAR using IntelliJ

  • In IntelliJ select File | Project Structure
  • Select Artifacts
  • Click the + button and select JAR | From modules with dependencies

Finally we need to build the JAR, so in IntelliJ select Build | Build Artifacts | Build.

Now we can execute our Hello World application from the JAR build folder, i.e.
C:\Development\HelloKotlinWorld\out\artifacts\HelloKotlinWorld_jar, by running the following at the command line

java -jar HelloKotlinWorld.jar

References

Kotlin Programming Language