Part 01 / Topic 6 of 9
kotlinc versus kotlin
These are two different programs, not two names for the same thing, and mixing them up is one of the most common early mistakes.
kotlinc |
kotlin |
|
|---|---|---|
| What it is | The compiler | The launcher |
| Input | One or more .kt source files |
A class name, a .jar, a .kts script, or a -e expression |
| Output | .class files, or a .jar with -d |
Whatever the program prints — it doesn't write new files |
Adds kotlin-stdlib to the classpath? |
No — that's a runtime concern, not a compile one | Yes, automatically (see section 8) |
kotlin can run the same compiled output four different ways, and all four were run for real to
confirm this:
$ kotlin -cp out01 _01_helloKt
Hello, Kotlin!
Welcome to Kotlin Step by Step
$ kotlin hello.jar
Hello, Kotlin!
Welcome to Kotlin Step by Step
$ kotlin 02_hello_script.kts
Hello, Kotlin!
Welcome to Kotlin Step by Step
$ kotlin -e '2 + 2'
4
The first form (kotlin -cp <folder> <ClassName>) is the Kotlin equivalent of java -cp <folder> <ClassName> — same idea, different launcher, with kotlin's extra trick of adding kotlin-stdlib
for you. The two launchers even word the same mistake differently:
$ java -cp /tmp/does-not-exist HelloKt
Error: Could not find or load main class HelloKt
Caused by: java.lang.ClassNotFoundException: HelloKt
$ kotlin -cp /tmp/does-not-exist HelloKt
error: could not find or load main class HelloKt
Same cause both times — the given classpath has no HelloKt in it — reported in each launcher's own
style: java's multi-line Error: plus a ClassNotFoundException, kotlin's single, lower-case
error: line.
Done here? The practice set for Part 1 — including the solutions — is on the part page.