- Kotlin command-line compiler
- Install the compiler
- Manual install
- SDKMAN!
- Homebrew
- Snap package
- Create and run an application
- Compile a library
- Run the REPL
- Run scripts
- Kotlin command-line compiler
- Install the compiler
- Manual install
- SDKMAN!
- Homebrew
- Snap package
- Create and run an application
- Compile a library
- Run the REPL
- Run scripts
- Get started with Kotlin/Native using the command-line compiler
- Write «Hello Kotlin/Native» program
- Compile the code from the console
- Working with the Command Line Compiler
- Downloading the compiler
- Manual Install
- SDKMAN!
- Homebrew
- MacPorts
- Creating and running a first application
- Compiling a library
- Running the REPL
Kotlin command-line compiler
Every Kotlin release ships with a standalone version of the compiler. You can download the latest version ( kotlin-compiler-1.7.20.zip ) from GitHub Releases.
Installing the command-line compiler is not an essential step to use Kotlin. A general way to write Kotlin applications is using an IDE — IntelliJ IDEA or Android Studio. They provide full Kotlin support out of the box without needing additional components. Learn how to get started with Kotlin in an IDE.
Install the compiler
Manual install
Unzip the standalone compiler into a directory and optionally add the bin directory to the system path. The bin directory contains the scripts needed to compile and run Kotlin on Windows, macOS, and Linux.
SDKMAN!
An easier way to install Kotlin on UNIX-based systems, such as macOS, Linux, Cygwin, FreeBSD, and Solaris, is SDKMAN!. It also works in Bash and ZSH shells. Learn how to install SDKMAN!.
To install the Kotlin compiler via SDKMAN!, run the following command in the terminal:
Homebrew
Alternatively, on macOS you can install the compiler via Homebrew:
brew update brew install kotlin
Snap package
If you use Snap on Ubuntu 16.04 or later, you can install the compiler from the command line:
sudo snap install --classic kotlin
Create and run an application
- Create a simple application in Kotlin that displays «Hello, World!» . In your favorite editor, create a new file called hello.kt with the following lines:
kotlinc hello.kt -include-runtime -d hello.jar
The -d option indicates the output path for generated class files, which may be either a directory or a .jar file. The -include-runtime option makes the resulting .jar file self-contained and runnable by including the Kotlin runtime library in it. To see all available options, run
Compile a library
If you’re developing a library to be used by other Kotlin applications, you can build the .jar file without including the Kotlin runtime in it:
kotlinc hello.kt -d hello.jar
Since binaries compiled this way depend on the Kotlin runtime, you should make sure the latter is present in the classpath whenever your compiled library is used.
You can also use the kotlin script to run binaries produced by the Kotlin compiler:
kotlin -classpath hello.jar HelloKt
HelloKt is the main class name that the Kotlin compiler generates for the file named hello.kt .
Run the REPL
You can run the compiler without parameters to have an interactive shell. In this shell, you can type any valid Kotlin code and see the results.
Run scripts
Kotlin can also be used as a scripting language. A script is a Kotlin source file ( .kts ) with top-level executable code.
import java.io.File // Get the passed in path, i.e. "-d some/path" or use the current path. val path = if (args.contains("-d")) args[1 + args.indexOf("-d")] else "." val folders = File(path).listFiles < file ->file.isDirectory() > folders?.forEach < folder ->println(folder) >
To run a script, pass the -script option to the compiler with the corresponding script file:
kotlinc -script list_folders.kts -- -d
Kotlin provides experimental support for script customization, such as adding external properties, providing static or dynamic dependencies, and so on. Customizations are defined by so-called Script definitions — annotated kotlin classes with the appropriate support code. The script filename extension is used to select the appropriate definition. Learn more about Kotlin custom scripting.
Properly prepared script definitions are detected and applied automatically when the appropriate jars are included in the compilation classpath. Alternatively, you can specify definitions manually by passing the -script-templates option to the compiler:
kotlinc -script-templates org.example.CustomScriptDefinition -script custom.script1.kts
For additional details, please consult the KEEP-75.
Kotlin 1.7
The Kotlin Standard Library provides comprehensive set of tools for managing collections groups variable number items (possibly zero) that are significant
Kotlin fixes a series of issues that Java suffers from: Null references are controlled by the type system.
Kotlin command-line compiler
Every Kotlin release ships with a standalone version of the compiler. You can download the latest version manually or via a package manager.
Installing the command-line compiler is not an essential step to use Kotlin. A general way to write Kotlin applications is using an IDE — IntelliJ IDEA or Android Studio. They provide full Kotlin support out of the box without needing additional components. Learn how to get started with Kotlin in an IDE.
Install the compiler
Manual install
- Download the latest version ( kotlin-compiler-1.9.0.zip ) from GitHub Releases.
- Unzip the standalone compiler into a directory and optionally add the bin directory to the system path. The bin directory contains the scripts needed to compile and run Kotlin on Windows, macOS, and Linux.
SDKMAN!
An easier way to install Kotlin on UNIX-based systems, such as macOS, Linux, Cygwin, FreeBSD, and Solaris, is SDKMAN!. It also works in Bash and ZSH shells. Learn how to install SDKMAN!.
To install the Kotlin compiler via SDKMAN!, run the following command in the terminal:
Homebrew
Alternatively, on macOS you can install the compiler via Homebrew:
Snap package
If you use Snap on Ubuntu 16.04 or later, you can install the compiler from the command line:
Create and run an application
- Create a simple application in Kotlin that displays «Hello, World!» . In your favorite editor, create a new file called hello.kt with the following lines:
The -d option indicates the output path for generated class files, which may be either a directory or a .jar file. The -include-runtime option makes the resulting .jar file self-contained and runnable by including the Kotlin runtime library in it. To see all available options, run
Compile a library
If you’re developing a library to be used by other Kotlin applications, you can build the .jar file without including the Kotlin runtime in it:
Since binaries compiled this way depend on the Kotlin runtime, you should make sure the latter is present in the classpath whenever your compiled library is used.
You can also use the kotlin script to run binaries produced by the Kotlin compiler:
HelloKt is the main class name that the Kotlin compiler generates for the file named hello.kt .
Run the REPL
You can run the compiler without parameters to have an interactive shell. In this shell, you can type any valid Kotlin code and see the results.
Run scripts
Kotlin can also be used as a scripting language. A script is a Kotlin source file ( .kts ) with top-level executable code.
import java.io.File // Get the passed in path, i.e. «-d some/path» or use the current path. val path = if (args.contains(«-d»)) args[1 + args.indexOf(«-d»)] else «.» val folders = File(path).listFiles < file ->file.isDirectory() > folders?.forEach < folder ->println(folder) >
To run a script, pass the -script option to the compiler with the corresponding script file:
Kotlin provides experimental support for script customization, such as adding external properties, providing static or dynamic dependencies, and so on. Customizations are defined by so-called Script definitions — annotated kotlin classes with the appropriate support code. The script filename extension is used to select the appropriate definition. Learn more about Kotlin custom scripting.
Properly prepared script definitions are detected and applied automatically when the appropriate jars are included in the compilation classpath. Alternatively, you can specify definitions manually by passing the -script-templates option to the compiler:
For additional details, please consult the KEEP-75.
Get started with Kotlin/Native using the command-line compiler
The Kotlin/Native compiler is available for macOS, Linux, and Windows. It is available as a command line tool and ships as part of the standard Kotlin distribution and can be downloaded from GitHub Releases. It supports different targets including Linux, macOS, iOS, and others. See the full list of supported targets. While cross-platform compilation is possible, which means using one platform to compile for a different one, in this Kotlin case we’ll be targeting the same platform we’re compiling on.
While the output of the compiler does not have any dependencies or virtual machine requirements, the compiler itself requires Java 1.8 or higher runtime.
Install the compiler by unpacking its archive to a directory of your choice and adding the path to its /bin directory to the PATH environment variable.
Write «Hello Kotlin/Native» program
The application will print «Hello Kotlin/Native» on the standard output. In a working directory of choice, create a file named hello.kt and enter the following contents:
Compile the code from the console
To compile the application use the downloaded compiler to execute the following command:
The value of -o option specifies the name of the output file, so this call should generate a hello.kexe (Linux and macOS) or hello.exe (Windows) binary file. For the full list of available compiler options, see the compiler options reference.
While compilation from the console seems to be easy and clear, it does not scale well for larger projects with hundreds of files and libraries. For real-world projects, it is recommended to use a build system and IDE.
Working with the Command Line Compiler
This tutorial walks us through creating a Hello World application using the command line compiler.
Downloading the compiler
Every release ships with a standalone version of the compiler. We can download it from GitHub Releases. The latest release is 1.1.4.
Manual Install
Unzip the standalone compiler into a directory and optionally add the bin directory to the system path. The bin directory contains the scripts needed to compile and run Kotlin on Windows, OS X and Linux.
SDKMAN!
An easier way to install Kotlin on UNIX based systems such as OS X, Linux, Cygwin, FreeBSD and Solaris is by using SDKMAN!. Simply run the following in a terminal and follow any instructions:
$ curl -s https://get.sdkman.io | bash
Next open a new terminal and install Kotlin with:
Homebrew
Alternatively, on OS X you can install the compiler via Homebrew.
$ brew update $ brew install kotlin
MacPorts
If you’re a MacPorts user, you can install the compiler with:
Creating and running a first application
- Create a simple application in Kotlin that displays Hello, World!. Using our favorite editor, we create a new file called hello.kt with the following:
$ kotlinc hello.kt -include-runtime -d hello.jar
The -d option indicates what we want the output of the compiler to be called and may be either a directory name for class files or a .jar file name. The -include-runtime option makes the resulting .jar file self-contained and runnable by including the Kotlin runtime library in it. If you want to see all available options run
Compiling a library
If you’re developing a library to be used by other Kotlin applications, you can produce the .jar file without including the Kotlin runtime into it.
$ kotlinc hello.kt -d hello.jar
Since binaries compiled this way depend on the Kotlin runtime you should make sure the latter is present in the classpath whenever your compiled library is used.
You can also use the kotlin script to run binaries produced by the Kotlin compiler:
$ kotlin -classpath hello.jar HelloKt
HelloKt is the main class name that the Kotlin compiler generates for the file named hello.kt .
Running the REPL
We can run the compiler without parameters to have an interactive shell. We can type any valid Kotlin code and see the results.
Using the command line to run scripts
Kotlin can also be used as a scripting language. A script is a Kotlin source file (.kts) with top level executable code.
import java.io.File val folders = File(args[0]).listFiles < file ->file.isDirectory() > folders?.forEach < folder ->println(folder) >
To run a script, we just pass the -script option to the compiler with the corresponding script file.
$ kotlinc -script list_folders.kts