Course map
Stage 1: Foundation
Stage 2: Intermediate
Stage 3: Advanced
Stage 4: DSA in Kotlin
Stage 5: Specialization

Part 01 / Topic 4 of 9

Compiling and running: kotlinc and java -jar

Every Kotlin program needs at least a top-level fun main():

fun main() {
    println("Hello, Kotlin!")
    println("Welcome to Kotlin Step by Step")
}

No class, no public static void main(String[] args) — Kotlin generates the ceremony Java requires by hand (more on exactly what it generates in section 7). println(...) prints one line.

The simplest way to run this is to ask kotlinc to bundle Kotlin's standard library straight into a runnable .jar:

$ kotlinc 01_hello.kt -include-runtime -d hello.jar
$ java -jar hello.jar
Hello, Kotlin!
Welcome to Kotlin Step by Step
  • kotlinc 01_hello.kt -include-runtime -d hello.jar compiles: -d hello.jar tells it to write a jar instead of loose .class files, and -include-runtime packs kotlin-stdlib inside that jar so it can run anywhere a JVM exists, with no separate Kotlin install needed on the machine that runs it.
  • java -jar hello.jar runs: standard java, no Kotlin-specific launcher required once everything is inside one jar.

Full program: examples/01_hello.kt.

Done here? The practice set for Part 1 — including the solutions — is on the part page.

Search the guide