Kotlin Development Environment and Installation
Kotlin Development Environment and Installation
The following are ways to create and run a Kotlin development environment.
- Run an editor available in a web browser
- Command-line interface (CLI) compiler
- Install IntelliJ IDEA
Run an Editor Available in a Web Browser
Among the methods mentioned above, the easiest and simplest way to run Kotlin is through a web browser.
Open the following URL in a web browser using Kotlin Playground provided by JetBrains.
When the screen opens, you will see a black screen and simple example code. On the right side, you can see icons for Run, Setting, Share, and Help.
The Play menu at the top also provides various hands-on exercises, examples, Koans, and more.
Command-Line Interface (CLI) Compiler
This is a way to compile by entering commands on your own computer. You can code with any editor you currently use.
Refer to the following URL to download or install the compiler.
Installing the Compiler
Manual Installation
For standalone use, extract the compiler archive 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!
To install on OS X, Linux, Cygwin, FreeBSD, and so on, do the following.
$ curl -s https://get.sdkman.io | bash
$ source ~/.sdkman/bin/sdkman-init.sh
$ sdk install kotlin
$ sdk install java
$ kotlinc -version
Homebrew
Alternatively, on OS X, you can install the compiler through Homebrew.
$ brew update
$ brew install kotlin
Compile and Run
Once the compiler is installed, run it and compile a Kotlin source file into a class file.
In any directory, write the following code and save the file as HelloWorld.kt. The .kt at the end of the file name is the extension for Kotlin source files.
fun main() {
println("Hello, world!")
}
Start a terminal, move to the directory where the file is saved, and enter the following command.
$ kotlinc HelloWorld.kt
$
The $ before the command is a symbol representing the prompt for convenience. If no message is printed and the next prompt appears, compilation succeeded. You can check with a command such as ls that a class file was created in the same directory.
$ ls
HelloWorldKt.class
The generated class file name is HelloWorldKt.class. Run this file. Execution can be done with the java command, just like Java.
$ java -cp . HelloWorldKt
Hello, world!
You can also compile it into a jar file and run it.
$ kotlinc HelloWorld.kt -include-runtime -d hello-world.jar
$ java -jar hello-world.jar
Hello, world!
Above, the first kotlinc command creates a jar file, and the java command runs it.
You can also compile to JavaScript.
$ kotlinc-js HelloWorld.kt -output hello-world.js
The compiled hello-world.js file looks like this.
if (typeof kotlin === 'undefined') {
throw new Error("Error loading module 'hello-world'. Its dependency 'kotlin' was not found. Please, check whether 'kotlin' is loaded prior to 'hello-world'.");
}this['hello-world'] = function (_, Kotlin) {
'use strict';
var println = Kotlin.kotlin.io.println_s8jyv4$;
function main() {
println('Hello, world!!!');
}
_.main = main;
main();
Kotlin.defineModule('hello-world', _);
return _;
}(typeof this['hello-world'] === 'undefined' ? {} : this['hello-world'], kotlin);
Installing IntelliJ IDEA
The last method introduced here is using IntelliJ IDEA, an IDE (integrated development environment). To download this IDE, open the following URL in a browser.
If you do not have a paid license, download the free Community Edition. After the download is complete, install it for your environment.
When you run it for the first time, the first screen will appear. Go to Plugins in the menu, find the Kotlin plugin, and install it. After plugin installation is complete, a message asking you to restart IntelliJ IDEA will appear. Restart it.
Now click [New Project]. When the new project wizard opens, enter the project name and other information in the form and create the project. After the project is created and the wizard disappears, the editor appears. Then add a Kotlin source file. To add a source file, select the src directory, right-click, and choose a Kotlin file to create the source file.
After writing code, select “[Build] > [Run…]” from the menu bar to compile and run it. Compilation will run at the bottom and the output result will be displayed.
Summary
This page introduced how to create, run, and use three Kotlin development environments.
Kotlin Playground, which can be easily used in a web browser, is good for simple experimental code. In general, Kotlin programming is done with IntelliJ IDEA. If you want to use another editor, you can compile with the CLI compiler.
Next, we will look at Kotlin’s basic syntax and features.