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.jarcompiles:-d hello.jartells it to write a jar instead of loose.classfiles, and-include-runtimepackskotlin-stdlibinside 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.jarruns: standardjava, 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.