Compile Kotlin to Java Bytecode and Run It

JAVING
ILLUMINATION
Published in
5 min readMar 1, 2021

--

In this article, I want to show how to use the Kotlin compiler from the ubuntu/Linux command line without an IDE to compile a “Hello World” program to java bytecode and then show different ways of running it.

Regardless that the latest IntelliJ IDE comes with Kotlin bundled, it’s always nice to understand how bytecode compilation works from the console perspective.

Before I start, I just want to remind you that Kotlin is one of the very few full-stack programming languages. So compiling to bytecode is just one of the many ways of using Kotlin but not the only one. Kotlin can also run as a script, we can transpile it to JavaScript, we can compile as Android or we can even compile it to native targets such as iOS, Windows, Linux, macOS, WebAssembly. In future articles, I might go deeper into other usages for Kotlin but in this one, we are going to compile Kotlin to java bytecode.

The first thing I am going to do is to install Kotlin. I am assuming that you have java already installed so I am not going to spend time talking about how to install it.

Installing kotlinc
In the official website there’s a nice brief set of installation instructions. I personally like the snap the easiness and convenience of snap install that’s what I will be using whenever possible.

Once that installation is complete it’s advised to verify using the version command.

The outcome of that should be something like this.

checking kotlin version

Setting Kotlin Path in environment variable

At this point we are ready to start using Kotlin from the command line but for ease of use, it’s advised to extract an environment variable that brings us to the location where the compiler is. We will see a little bit later why this is useful.

I personally like certain environment variables to have system wide scope so place them in /etc/environment

variable defined inside /etc/environment

Now that I have added the path to kotlin (KOTLIN_PATH=”/snap/bin/kotlin”) inside the env var, all I need to do is make sure it is included in the system PATH variable. We could also do this in that same file but for controlling the PATH I like using my users ~/.bashrc file. Other people like maybe doing it other way but this is the way I like, feel free to do it differently if you prefer.

So in the same way as you would add JAVA_HOME or any other environment variable, just append KOTLIN_PATH to the end of the PATH variable in the ~/.bashrc file

adding KOTLIN_PATH to ~/.bashrc

After editing those files, remember to reload both the /etc/environment and the ~/.bashrc files

Hello World example

Create a simple hello world program and add save it to a file called HelloWorld.kt

creating a Kotlin Hello World program using the command line

That command above will echo the Kotlin code into the HelloWorld.kt file. So now in order to compile that into java bytecode, we need to use the kotlinc compiler we installed earlier.

Compiling a Kotlin program using Kotlinc

When that’s executed a .jar file will be created an now we can run it using the java command.

running Kotlin program using the java command and the classpath option

If all we did is ok that should print in the terminal “Hello World” for us.

Since our program does not contain a class, it is only a main() method the Kotlin compiler will automatically for us create a class but instead of using the .kt extension it just uses a Kt suffix. So that’s why we are using HelloWorldKt in the above command.

We can also use the -jar option to run the program instead of using the -classpath option. This is possible because when the program was compiled the compiler added a manifest file to the jar file.

screenshoot showing all the contents of the jar file that was created during compilation.

In the above image, we can see the MANIFEST.MF file and inside it the descriptor pointing to the class containing the main method that the compiler created.

The content of the Manifest file

Now show how to run this program using the -jar option instead of the -classpath option. As explained already, because the compiler added the Manifest file, java JVM knows where to find the main class and therefore run the program.

Running Kotlin program with -jar

There’s one thing to notice in the ways I executed the program so far. This Hello World example it’s too trivial and it does not use any specific Kotlin standard library. If we were to use some of the kotlin-stdlib.jar functions in our program, the executions we did would have resulted in a java. lang.NoClassDefFoundError so in order to prevent this we would have to make sure to add that library to the classpath at execution time using the -classpath option.

Running Kotlin Program and including Kotlin standard library

For a nontrivial example of the Kotlin program note that we would have to add the path to the standard kotlin library and to do so we have to help ourselves from the environmental variable we created at the beginning.

The last way I want to show to run our jar file is using the kotlin tool.

running jar using the Kotlin tool

So what is better or what should we be using? There’s no better or worse. A suggestion is to use the java tool if we are mostly programming in Java but using a bit of Kotlin on our project sporadically. But if we are mainly suing Kotlin then probably using the kotlin tool is more convenient since it requires less configuration.

--

--

JAVING
ILLUMINATION

The present continuous form of “to program in Java”.