LinuxCommandLibrary

kotlinc

Compile Kotlin source code to bytecode

TLDR

Start a REPL (interactive shell)

$ kotlinc
copy

Compile a Kotlin file
$ kotlinc [path/to/file.kt]
copy

Compile several Kotlin files
$ kotlinc [path/to/file1.kt path/to/file2.kt ...]
copy

Execute a specific Kotlin Script file
$ kotlinc -script [path/to/file.kts]
copy

Compile a Kotlin file into a self contained jar file with the Kotlin runtime library included
$ kotlinc [path/to/file.kt] -include-runtime -d [path/to/file.jar]
copy

SYNOPSIS

kotlinc [options] <source files> [-d <output path>]

PARAMETERS

-d <path>
    Destination directory or JAR file for output

-classpath <path>
    Classpath for dependency modules (alias: -cp)

-include-runtime
    Package compiled classes and Kotlin runtime into JAR

-module-name <name>
    Name of the generated Kotlin module

-jvm-target <version>
    Target JVM bytecode version (e.g., 1.8, 11)

-no-stdlib
    Don't automatically include Kotlin standard library

-verbose
    Enable verbose logging of compilation process

-nowarn
    Generate no warnings

-Xlint
    Enable or disable specific lint warnings

-help
    Display compiler help and options

-version
    Print compiler version

-all-warnings-as-errors
    Treat all warnings as errors

DESCRIPTION

kotlinc is the official command-line compiler for the Kotlin programming language, developed by JetBrains. It transforms Kotlin source files (.kt or .kts) into platform-specific bytecode, primarily targeting the Java Virtual Machine (JVM) to produce .class files or executable JARs. Kotlin is designed as a concise, interoperable alternative to Java, supporting modern features like null safety, coroutines, and extension functions.

Basic usage involves listing source files after options: kotlinc file.kt -d output/ compiles to a directory, while kotlinc -include-runtime -d app.jar *.kt creates a runnable JAR with embedded runtime libraries. It supports multi-file compilation, classpath management for dependencies, and targets like JS or Native via flags.

kotlinc integrates seamlessly with Java codebases, enabling gradual adoption. It performs type inference, optimizes code, and emits warnings for potential issues. For large projects, it's typically used via build tools like Gradle or Maven, but excels standalone for scripts and small apps. Requires a compatible JDK (8+ recommended). Output is JVM-compatible, runnable via java command.

CAVEATS

Requires JDK 1.8+ installed; mismatched Kotlin/JDK versions may cause issues. Large projects better suited for Gradle/Maven. No built-in support for multi-module without explicit classpath.

SIMPLE EXAMPLE

kotlinc hello.kt -include-runtime -d hello.jar
java -jar hello.jar

SCRIPT COMPILATION

Use kotlinc -script file.kts for Kotlin scripts (.kts)

HISTORY

Developed by JetBrains; announced 2011, Kotlin 1.0 stable release February 2016. Became Google Android preferred language 2017. Open-source under Apache 2.0 since 2016; kotlinc evolves with Kotlin versions (current ~1.9+).

SEE ALSO

javac(1), gradle(1), scala(1)

Copied to clipboard