Using the command line to compile and run Kotlin code
To write and execute code written in Kotlin, you will need its runtime and the compiler. At the time of writing, version 1.1 milestone 4 is available (the stable release is 1.0.6). Every runtime release comes with its own compiler version. To get your hands on it, navigate to https://github.com/JetBrains/kotlin/releases/tag/v1.1-M04, scroll to the bottom of the page, and download and unpack the ZIP archive kotlin-compiler-1.1-M04.zip
to a known location on your machine. The output folder will contain a subfolder bin
with all the scripts required to compile and run Kotlin on Windows, Linux, or OS X. Now you need to make sure the bin
folder location is part of your system PATH in order to call the kotlinc
without having to specify the full path.
If your machine runs Linux or OS X, there is an even easier way to install the compiler by using sdkman
. All you need to do is execute the following commands in a terminal:
$ curl -s https://get.sdkman.io | bash $ bash $ sdk install kotlin 1.1-M04
Alternatively, if you are using OS X and you have homebrew
installed, you could run these commands to achieve the same thing:
$ brew update
$ brew install kotlin@1.1-M04
Now that all of this is done, we can finally write our first Kotlin code. The application we will be writing does nothing else but display the text Hello World!
on the console. Start by creating a new file named HelloWorld.kt
and type the following:
fun main(args: Array<String>) { println("Hello, World!") }
From the command line, invoke the compiler to produce the JAR assembly (include-runtime
is a flag for the compiler to produce a self-contained and runnable JAR by including the Kotlin runtime into the resulting assembly):
kotlinc HelloWorld.kt -include-runtime -d HelloWorld.jar
Now you are ready to run your program by typing the following on your command line; it is assumed your JAVA_HOME
is set and added to the system path:
$ java -jar HelloWorld.jar
The code is pretty straight forward. It defines the entry point function for your program, and in the first and only line of code, it prints the text to the console.
If you have been working with the Java or Scala languages, you might raise an eyebrow because you noticed the lack of the typical class that would normally define the standard static
main program entry point. How does it work then? Let's have a look at what actually happens. First, let's just compile the preceding code by running the following command. This will create a HelloWorld.class
in the same folder:
$ kotlinc HelloWorld.kt
Now that we have the bytecode generated, let's look at it by using the javap
tool available with the JDK (please note that the file name contains a suffix Kt
):
$ javap -c HelloWorldKt.class
Once the execution completes, you should see the following printed on your terminal:
Compiled from "HelloWorld.kt" public final class HelloWorldKt { public static final void main(java.lang.String[]); Code: 0: aload_0 1: ldc #9 // String args 3: invokestatic #15 // Method kotlin/jvm/internal/Intrinsics.checkParameterIsNotNull:(Ljava/lang/Ob ject;Ljava/lang/String;)V 6: ldc #17 // String Hello, World! 8: astore_1 9: nop 10: getstatic #23 // Field java/lang/System.out:Ljava/io/PrintStream; 13: aload_1 14: invokevirtual #29 // Method java/io/PrintStream.println:(Ljava/lang/Object;)V 17: return }
You don't have to be an expert in bytecode to understand what the compiler has actually done for us. As you can see on the snippet, a class has been generated for us, and it contains the program entry point with the instructions to print Hello World! to the console.
I would not expect you to work with the command line compiler on a daily basis; rather, you should use the tools at hand to delegate this, as we will see shortly.