Write your first code in Kotlin

Once you have installed the Kotlin Command-line Compiler, you may be wondering how to write your first code in Kotlin. In this article, we would also discuss how to create a .jar executable from it, which includes a class file as well as Kotlin run-time library.

The source code written in Kotlin is saved in a file with .kt extension.

Write your first “Hello World!” code in Kotlin

Open a text-editor and save the file as HelloWorld.kt. We have used nano text-editor, choose one which you are comfortable with.

nano HelloWorld.kt

Next, insert the following code –

fun main(args: Array<String>) {
       println("Hello, World!")
}

where,

fun – it means function, just to let compiler know that we are going to call a function.
main – its the function main() which is to be called.
args – is the name of our variable.
Array<String> – tells the compiler that variable args is an Array of type String.
println() – print line function.
“Hello, World!” – the message we would like to print.

Save the HelloWorld.kt and exit.

So far, we have created a file with .kt extension and then inserted our code understood by the Kotlin Compiler. Now, we will compile the code with the help of following command –

kotlinc HelloWorld.kt

Notice that, it has created a file – HelloWorldKt.class and a folder META-INF with main.kotlin_module. Compiling the code puts the main function inside the HelloWorldKt.class file, which later could run inside a Java Virtual Machine (JVM).

If you would like to see byte-code inside the HelloWorldKt.class then you may have to use javap command. javap command is basically a command line utility which lets us see the code so generated inside the .class file.

javap -c HelloworldKt.class

Output should resemble –

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/Object;Ljava/lang/String;)V
6: ldc #17 // String Hello, World!
8: astore_1
9: iconst_0
10: istore_2
11: getstatic #23 // Field java/lang/System.out:Ljava/io/PrintStream;
14: aload_1
15: invokevirtual #29 // Method java/io/PrintStream.println:(Ljava/lang/Object;)V
18: return
}

Make Kotlin Compiler produce a .jar executable

The executable so produced will include Kotlin run-time library as well as HelloworldKt.class. Thus, eliminating the need to distribute Kotlin run-time along-with .jar executable. To make one, run the following in terminal –

kotlinc HelloWorld.kt -include-runtime -d HelloWorld.jar

where,

-include-runtime – asks the compiler to include Kotlin run-time library to make .jar executable.
-d – for output compiled as .jar executable

Lastly, run the following to execute .jar file –

java -jar HelloWorld.jar

This should get us expected output i.e. Hello, World!

In conclusion, we have discussed how to write our first Kotlin code and then compile it to a .jar executable.