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 or directories>
kotlinc -script <script file> [arguments]

PARAMETERS

-d
    Specifies the destination for compiled classes or the name of the output JAR file.

-classpath , -cp
    Defines the search path for application classes and resources, typically used for external libraries.

-include-runtime
    Bundles the Kotlin runtime library into the resulting JAR file, making it self-contained and runnable.

-jvm-target
    Sets the target JVM bytecode version (e.g., 1.8, 11, 17) for compatibility.

-version
    Prints the Kotlin compiler version and exits.

-help
    Displays a summary of common command-line options.

-script
    Instructs the compiler to interpret the input as a Kotlin script (.kts) and execute it.

-nowarn
    Suppresses all compiler warnings.

-verbose
    Enables verbose output, showing detailed compilation process information.

-module-name
    Specifies the name of the module being compiled, used for module-info.class and JAR manifests.

DESCRIPTION

kotlinc is the command-line compiler for the Kotlin programming language, developed by JetBrains. It processes Kotlin source files (.kt and .kts) and translates them into various target formats.

Its primary use is compiling to JVM bytecode (.class files or executable JARs), enabling Kotlin applications to run on the Java Virtual Machine. Additionally, it supports compilation to JavaScript for web environments and native binaries via Kotlin/Native for platform-specific applications. The compiler performs essential tasks like type checking, error detection, and bytecode generation. It's a fundamental tool for developers building Kotlin projects outside of IDEs or higher-level build tools like Gradle or Maven, offering direct control over the compilation process and output.

CAVEATS

For large or complex projects, kotlinc is typically invoked indirectly via build tools like Gradle or Maven, which manage dependencies, incremental compilation, and project structure more effectively. Direct kotlinc usage can be cumbersome for multi-module projects, and compilation can be memory-intensive for very large codebases.

CROSS-PLATFORM TARGETS

Beyond the JVM, kotlinc can also compile Kotlin code to JavaScript (for web development) and native binaries (using Kotlin/Native for platforms like iOS, macOS, Windows, and Linux), showcasing its versatility as a multi-platform language compiler.

INTEGRATION WITH BUILD TOOLS

While kotlinc can be used directly, it's most commonly integrated into build systems like Gradle or Maven. These tools provide comprehensive dependency management, incremental compilation, test execution, and deployment capabilities, streamlining the development workflow for Kotlin projects, making direct kotlinc calls less frequent in day-to-day development.

HISTORY

Kotlin was developed by JetBrains and first publicly released in 2011. The kotlinc compiler has been the core component since its inception, continuously evolving with the language. It gained significant popularity after Google announced it as a first-class language for Android development in 2017, leading to wider adoption and continuous improvements in the compiler's performance and features.

SEE ALSO

javac(1), java(1), gradle(1), mvn(1)

Copied to clipboard