Kotlin Get Started
You can follow this whole course in the browser — every ▶ Run button compiles your code on a real Kotlin compiler. Sooner or later, though, you will want Kotlin on your own computer. This chapter shows the three common ways to set it up.
Option 1: IntelliJ IDEA (recommended)
IntelliJ IDEA from JetBrains has first-class Kotlin support built in — the free Community Edition is enough.
- Download and install IntelliJ IDEA from jetbrains.com.
- Choose New Project, pick Kotlin as the language and Gradle (Kotlin DSL) or IntelliJ as the build system.
- IntelliJ downloads a JDK if you don't have one, and creates
src/main/kotlin/Main.kt. - Click the green ▶ arrow next to
fun main()to run it.
The generated file looks like this:
fun main() {
println("Hello World!")
}Option 2: Android Studio
If your goal is Android apps, install Android Studio (built on IntelliJ). Choose New Project → Empty Activity; the wizard creates a Kotlin project with Jetpack Compose. You will learn more in the Kotlin for Android chapter.
Tip: Android Studio can also run plain Kotlin files: right-click a file containing
fun main()and choose Run.
Option 3: The command-line compiler
Kotlin needs a Java runtime (JDK 17 or newer is a good choice). Then install the compiler, for example with SDKMAN! on Linux/macOS or Homebrew on macOS:
sdk install kotlin # or: brew install kotlin
kotlinc -versioninfo: kotlinc-jvm 2.4.21 (JRE 21.0.5+11-LTS)
Your version numbers will differ. Now write a program in a file called hello.kt:
fun main() {
println("It works!")
}Compile it into a runnable JAR (the -include-runtime flag bundles the Kotlin standard library) and run it with java:
kotlinc hello.kt -include-runtime -d hello.jar
java -jar hello.jarIt works!
You can also run small scripts directly. Files ending in .main.kts or .kts are Kotlin scripts — top-level statements without a main function:
echo 'println("2 + 3 = ${2 + 3}")' > quick.main.kts
kotlin quick.main.kts2 + 3 = 5
The tools at a glance
| Tool | Purpose |
|---|---|
kotlinc |
the Kotlin compiler (and an interactive REPL when started without arguments) |
kotlin |
runs compiled classes, JARs or .main.kts scripts |
| Gradle / Maven | build tools for real projects (dependencies, tests, packaging) |
| IntelliJ IDEA / Android Studio | IDEs with completion, refactoring and a debugger |
Check your setup in the playground
Whatever you install, the code is the same. This program prints the Kotlin version it was compiled with — run it here to see the version used by ExcelTAD.
fun main() {
println("Hello from Kotlin ${KotlinVersion.CURRENT}")
println("At least 2.0? ${KotlinVersion.CURRENT.isAtLeast(2, 0)}")
}Hello from Kotlin 2.4.21
At least 2.0? true
KotlinVersion.CURRENT is part of the standard library, so it always reports the version of the runtime you are using.
Project layout
A typical Gradle project for Kotlin looks like this:
my-app/
build.gradle.kts build script (written in Kotlin!)
settings.gradle.kts
src/
main/kotlin/Main.kt your code
test/kotlin/ your testsNote: The file name does not have to match anything inside it. A file may contain several classes and functions; by convention a file holding one class is named after that class.
Test yourself
What does Kotlin on the JVM need in order to run a compiled program?
- Node.js
- A Java runtime (JDK/JRE)
- Python 3
- Nothing at all
What does the -include-runtime flag of kotlinc do?
- Includes debugging symbols
- Bundles the Kotlin standard library into the JAR so
java -jarcan run it - Runs the program after compiling
- Adds Android support