TExcelTAD
Home › Kotlin › Kotlin Get Started

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.

  1. Download and install IntelliJ IDEA from jetbrains.com.
  2. Choose New Project, pick Kotlin as the language and Gradle (Kotlin DSL) or IntelliJ as the build system.
  3. IntelliJ downloads a JDK if you don't have one, and creates src/main/kotlin/Main.kt.
  4. Click the green ▶ arrow next to fun main() to run it.

The generated file looks like this:

kotlin
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:

bash
sdk install kotlin        # or: brew install kotlin
kotlinc -version
info: 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:

kotlin
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:

bash
kotlinc hello.kt -include-runtime -d hello.jar
java -jar hello.jar
It 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:

bash
echo 'println("2 + 3 = ${2 + 3}")' > quick.main.kts
kotlin quick.main.kts
2 + 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.

kotlin · try it yourself
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:

text
my-app/
  build.gradle.kts        build script (written in Kotlin!)
  settings.gradle.kts
  src/
    main/kotlin/Main.kt   your code
    test/kotlin/          your tests

Note: 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?

  1. Node.js
  2. A Java runtime (JDK/JRE)
  3. Python 3
  4. Nothing at all

What does the -include-runtime flag of kotlinc do?

  1. Includes debugging symbols
  2. Bundles the Kotlin standard library into the JAR so java -jar can run it
  3. Runs the program after compiling
  4. Adds Android support