Using Kotlin from CLI on Mac

Davi Diório Mendes
Apr 3, 2019 · 4 min read
Kotlin code on vim

Hello everyone! I don’t know about you guys, but I really love working on terminal. One of these days I have started studying Kotlin and I soon noticed that you should install IntelliJ and blah blah blah. I have anything against IntelliJ. Really love it actually. But I like the fun in doing things by hand too.

Were I working on a real Kotlin product or a more complex project I would use the IntelliJ for sure. It would break a leg for me. But for simple studies and the fun, I got curious whether I could edit it on vim and compile/execute by hand on terminal.

Yes! Yes, we can! And it’s not hard at all! Here comes a tutorial for you that like the fun of coding & running on terminal. Tested on Mac. If you use Linux/Windows I’ll let some reference on the end and you can dig a bit on yourself.


Java SE Development Kit

If you still don’t have the jdk installed on your system install it from the source you like. I installed the official from oracle website: Java SE Downloads

Just run the installer, it is pretty simple. After it is done you can check on your terminal with $ java -version.

Verifying java installation

If you want more details about Java installation you can check on oracle website: How do I install Java for my Mac?

Kotlin Compiler

Installing Java is just a pre-requisite for the core of this story: the Kotlin Compiler. Now that we have jdk installed we can attack the main course.

The Kotlin Official documentation has an entire tutorial on how to install its compiler in several ways for several operational systems. For mac you can go with brew:

$ brew install kotlin

Just let it be brew and test the installation by checking kotlin version.

Verifying kotlin installation

Congrats! Now we can compile and run our kotlin studies directly on terminal!


And now, finally, the great, the fabulous, the magnificent. The…

Hello, World!

The “Hello, World!” is usually a very simple program that just prints the text “Hello, World!”. With it we can know the very basic steps to compile any program. Now we will create our Kotlin “Hello, World!” to test the kotlin compiler we just installed.

Below we have the code. Write it with your favorite text editor, save to your favorite folder and let’s put our compiler to rock.

I particularly used macvim with the VimBox setup. But this is a subject for another story.

We will compile our kotlin code to a jar file and run it with the JVM. If you want to check more options and usages of the compiler you can refer to official documentation but I will leave the basics here.

Open your terminal and change to that favorite directory where you saved our hello.kt. Now run these commands:

$ kotlinc hello.kt -include-runtime -d hello.jar
$ java -jar hello.jar
Compile & run Kotlin Hello, World

Now we have a jar file with our code plus the kotlin runtime. If you omit the -include-runtime flag you must have the kotlin runtime in your path or you can use the jar as a lib in your project.

You can also just drop a $ kotlinc and start a Kotlin REPL session.

Using Kotlin REPL

See you!

I hope you liked this post. If you have any doubt or trouble on this tutorial, please let me know! Any ideas for what you want to see from terminal, please let me know as well! Thank you for your time and have fun with my sources.

Beware of the App

A place to talk about my studies, share some tips and help…

Davi Diório Mendes

Written by

Ex android dev. Now working in the clouds. From Brazil 🇧🇷

Beware of the App

A place to talk about my studies, share some tips and help everyone that I can.

More From Medium

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade