Hello World in Kotlin

Our first program will print the classic “hello world” message. Here’s the full source code.

fun main() {
    println("hello world")
}

To run the program, put the code in a file with a .kt extension, such as hello-world.kt, and use the Kotlin compiler to run it.

$ kotlinc hello-world.kt -include-runtime -d hello-world.jar
$ kotlin -classpath hello-world.jar HelloWorldKt
hello world

Sometimes we’ll want to build our programs into binaries that can be executed directly. In Kotlin, you can create an executable JAR file.

First, compile the code and then package it into a JAR file:

$ kotlinc hello-world.kt -include-runtime -d hello-world.jar
$ ls
hello-world.jar    hello-world.kt

We can then execute the JAR file directly using the java -jar command.

$ java -jar hello-world.jar
hello world

Now that we can run and build basic Kotlin programs, let’s learn more about the language.