Explain it simply
Setup, simply
To run Kotlin code you need something that compiles it. There are three roads. The Playground in your browser needs no installation. An IDE (IntelliJ IDEA or Android Studio) is a full workshop for real projects. On the command line you install a JDK and `kotlinc` and compile by hand.
Picking just one for now is enough, because all three run exactly the same code. Still, trying the command line once shows you what an IDE does behind the scenes.
Problem → Why → Solution
The problem: the code is right but nothing runs
You copy a program from a lesson exactly, save it as `hello.kt` and try to compile it, and the terminal says it does not know `kotlinc`. You fix that, the compile succeeds, and then running the jar crashes with `NoClassDefFoundError`. If you have never seen these messages, it is easy to assume the code is wrong and start editing code that was correct all along.
$ kotlinc hello.kt -include-runtime -d hello.jar
bash: kotlinc: command not found
# later, after installing, but compiling without -include-runtime:
$ kotlinc hello.kt -d hello.jar
$ java -jar hello.jar
Exception in thread "main" java.lang.NoClassDefFoundError: kotlin/jvm/internal/IntrinsicsWhy these errors happen
Both are about the environment, not the code. The shell looks for a command only in the folders listed in PATH, so if the folder holding `kotlinc` is not there, the command is not found. The second error appears because a jar built without `-include-runtime` does not contain the Kotlin standard library classes.
Core concept: from source through a jar to the JVM
First choose the road that fits you. The table below says what each one is best for. Every example in this course is a single file with `fun main()`, so all three roads work.
| Road | What you need | Best for |
|---|---|---|
| Kotlin Playground | Just a browser (play.kotlinlang.org) | Trying examples quickly |
| IntelliJ IDEA / Android Studio | Download an IDE | Real projects, debugging, refactoring |
| Command line | A JDK and kotlinc | Understanding the compiler, scripts and CI |
Now the command-line road, step by step. Check the result of each step before moving on, and when something goes wrong you will know right away which step caused it.
1. Install and check a JDK
Both the Kotlin compiler and the programs it produces run on the JVM, so you need a JDK (Java Development Kit). Open a terminal and run `java -version`; a line with a version number should appear.
2. Install kotlinc
Use SDKMAN (`sdk install kotlin`), Homebrew (`brew install kotlin`), or download the compiler zip from the Kotlin GitHub releases page, unzip it and add its `bin` folder to PATH.
3. Verify in a new terminal
Open a new terminal and run `kotlinc -version`. A line such as `info: kotlinc-jvm 2.x.x ...` should appear. A PATH change only takes effect in terminals opened afterwards.
4. Compile and run
Save `hello.kt`, compile it with `kotlinc hello.kt -include-runtime -d hello.jar`, then run it with `java -jar hello.jar`. The compiler can take a few seconds even for a tiny file, which is normal.
Here is what the parts of the compile command do. `-d hello.jar` names the output and makes it a jar file. `-include-runtime` copies the Kotlin standard library into the jar, so that one jar runs on any machine that has a JDK. The top-level `main` in `hello.kt` ends up in a class called `HelloKt`, and the compiler records it as the main class in the jar's manifest, which is how `java -jar` knows where to start.
$ java -version
$ kotlinc -version
$ kotlinc hello.kt -include-runtime -d hello.jar
$ java -jar hello.jar- hello.kt: your source
- kotlinc: check and compile
- hello.jar: bytecode and runtime
- java -jar: the JVM runs it
When an error appears, look at where it stopped: in `kotlinc` or in `java`. That alone splits the problem in half.
General pattern: the command-line workflow
java -version
kotlinc -version
kotlinc SourceFile.kt -include-runtime -d name.jar
java -jar name.jarThe first two lines check that the tools exist. The third compiles a `.kt` file into a jar and the last runs that jar on the JVM. You can pick any file name and jar name you like.
A program that checks your setup
Write this program into `hello.kt` and run it with the four commands above. In the Playground, paste it and press Run. On Kotlin 2.x the output matches what is shown below; on a later major version the first line changes.
fun main() {
val kotlin = KotlinVersion.CURRENT
println("Kotlin major version: ${kotlin.major}")
println("Kotlin 2 or newer: ${kotlin.isAtLeast(2, 0)}")
println("Running on a JVM: ${System.getProperty("java.version") != null}")
}Code breakdown
- The running Kotlin version
val kotlin = KotlinVersion.CURRENT`KotlinVersion.CURRENT` is an object from the standard library holding the major, minor and patch numbers of the Kotlin that is running. This line stores it under the name `kotlin`.
- Reading one property
println("Kotlin major version: ${kotlin.major}")`kotlin.major` returns the major version number. Because the expression contains a dot, it goes into the string template inside braces: `${ }`.
- A Boolean check
println("Kotlin 2 or newer: ${kotlin.isAtLeast(2, 0)}")`isAtLeast(2, 0)` returns `true` when the version is 2.0 or higher. This course is written for Kotlin 2.x, so you should see `true` on your machine.
- Calling a Java API
System.getProperty("java.version")`System` is a Java class, and Kotlin can call it directly without an import. Comparing the result with `!= null` turns it into a `true` or `false` that says whether a JVM is running.
Kotlin major version: 2
Kotlin 2 or newer: true
Running on a JVM: true- Save hello.kt
- kotlinc checks and compiles
- hello.jar is written
- java -jar runs it and prints
It compiles
kotlinc finishes silently and hello.jar appears in the folder
It does not compile
Error lines with file, line and column; no jar is written
What a Gradle project looks like
Real projects rarely call `kotlinc` by hand. A build tool called Gradle manages compiling, dependencies and running. The New Project wizard in an IDE writes a `build.gradle.kts` file like the one below for you. Source code lives in `src/main/kotlin`, and the `main` in `src/main/kotlin/Main.kt` is found as the class `MainKt`.
plugins {
kotlin("jvm") version "2.1.21"
application
}
repositories {
mavenCentral()
}
kotlin {
jvmToolchain(21)
}
application {
mainClass.set("MainKt")
}Keep the version number the wizard writes; the 2.1.21 above is just an example. To run the project from a terminal, use `./gradlew run` inside the project folder (`gradlew run` on Windows). Gradle gets a lesson of its own later in the course.
Troubleshooting the first-day errors
| Message | Likely cause | Fix |
|---|---|---|
| kotlinc: command not found | The bin folder is not on PATH | Add it to PATH, open a new terminal |
| error: source file or directory not found: hello.kt | Wrong folder | `cd` into the folder that holds the file |
| NoClassDefFoundError: kotlin/jvm/internal/Intrinsics | Jar built without -include-runtime | Rebuild with the flag |
| Error: Could not find or load main class hello.jar | java was called without -jar | Write `java -jar hello.jar` |
In real projects
How teams handle setup in practice
On a team project nobody types `kotlinc` by hand. You clone the repository and run `./gradlew build`. The Kotlin version and the JDK version are declared in `build.gradle.kts`, so every developer and the CI server build with the same tools, which cuts down on 'it works on my machine' problems.
Still, understanding the kotlinc to jar to java route from this lesson matters. When a build fails unexpectedly, Gradle is running those same stages behind the scenes, so you can work out which stage has the problem. When you package a Kotlin app into a Docker image, you will also see that all it needs is a JDK and one jar.
Common mistakes
Common setup mistakes
1. Using an old terminal after changing PATH
`kotlinc: command not found` keeps appearing even after installing. A terminal that was already open keeps the old PATH. Close it and open a new one; for VS Code's built-in terminal, restart VS Code as well.
2. Forgetting -include-runtime
A program that only calls `println` can run even from a jar built without the runtime, so you may not notice the problem. Only once a program has functions with parameters or uses library types such as lists does it fail with `NoClassDefFoundError`. Sending the jar to someone else exposes it too, so always add the flag.
3. Typing the wrong run command
Typing `java hello.jar` gives `Error: Could not find or load main class hello.jar`, because without `-jar` java treats `hello.jar` as a class name. Write `java -jar hello.jar` instead.
Best practices
Best practices
- Make one folder per exercise so files do not get mixed up.
- Use the Playground for a quick look and move to an IDE once the code is longer than about ten lines.
- After every install, check `java -version` and `kotlinc -version` first; it avoids many errors that look like lesson problems.
Mini challenge
Mini challenge: a setup report
Write `Report.kt`, which prints your Kotlin version, Java version and number of CPU cores, then compile it into a jar and run it.
- Print a heading line `Setup report`
- Print the Kotlin version using `KotlinVersion.CURRENT`
- Print the Java version using `System.getProperty("java.version")`
- Print the core count using `Runtime.getRuntime().availableProcessors()`, then build a jar with `-include-runtime` and run it
fun main() {
// TODO: print the heading "Setup report"
// TODO: print the Kotlin version (KotlinVersion.CURRENT)
// TODO: print the Java version (System.getProperty with "java.version")
// TODO: print how many CPU cores are available
}Expected: Varies by machine, for example: Setup report Kotlin: 2.1.21 Java: 21.0.13 CPU cores: 8
Show hint
Put each value into a template, as in `println("Kotlin: ${KotlinVersion.CURRENT}")`. Kotlin can call Java's `Runtime.getRuntime()` directly, and `availableProcessors()` returns an Int.
Show solution
fun main() {
println("Setup report")
println("Kotlin: ${KotlinVersion.CURRENT}")
println("Java: ${System.getProperty("java.version")}")
println("CPU cores: ${Runtime.getRuntime().availableProcessors()}")
}Quick quiz 1 / 3
Quick quiz 2 / 3
Quick quiz 3 / 3
What to remember
- The Playground, an IDE and the command line all run the same code, so pick the one that fits you.
- The command line needs a JDK and kotlinc; check `java -version` and `kotlinc -version` in a new terminal.
- Compile with `kotlinc hello.kt -include-runtime -d hello.jar` and run with `java -jar hello.jar`.
- `command not found` is a PATH problem and `NoClassDefFoundError` is a jar built without the runtime.
Kotlin releases — Kotlin