thecodex.expert · The Codex Family of Knowledge
Kotlin

Setup and Tooling

kotlinc compiles a single file directly from the terminal — but the moment a project has more than one file or a single dependency, Gradle becomes the real starting point.

Kotlin 2.1 Gradle for real projects Last verified:
Canonical Definition

kotlinc, the standalone command-line compiler, compiles a .kt file directly into a runnable .jar — fine for small standalone examples, but real projects use Gradle with the Kotlin Gradle plugin (or Maven, less commonly) to manage dependencies, multi-file compilation, and packaging. IntelliJ IDEA, built by the same company that makes Kotlin, offers the most complete IDE experience, though Android Studio (itself based on IntelliJ) is the standard choice for Android development specifically.

Compiling directly with kotlinc

-include-runtime bundles the Kotlin standard library into the output jar, so it runs anywhere a JVM is installed, without needing Kotlin itself present on the target machine.

Kotlinterminal
$ kotlinc hello.kt -include-runtime -d hello.jar
$ java -jar hello.jar
Hello, World!

Gradle: the standard for real projects

build.gradle.kts (Kotlin-scripted Gradle, now the common default) declares the Kotlin plugin and dependencies; ./gradlew build compiles, tests, and packages the whole project in one command.

Kotlinbuild.gradle.kts
plugins {
    kotlin("jvm") version "2.4.0"
}

dependencies {
    testImplementation(kotlin("test"))
}
Kotlinterminal
$ ./gradlew build

Kotlin scripting: .kts files for quick scripts

A .kts file runs directly as a script without a formal project or build step — genuinely useful for quick automation tasks, similar in spirit to a Python or shell script.

Kotlinterminal
$ kotlinc -script myscript.kts

Sources

1
JetBrains. Kotlin Documentation, "Working with command-line compiler," kotlinlang.org/docs/command-line.html.