Install, compile and run Kotlin from command line

Prerequsite: Kotlin runs on jvm, which means java runtime is required to run it.

Install sdkman from command line if it is not installed yet.

curl -s | bash

Install Kotlin using the command sdk from the command line.

sdk install kotlin

Create a new file hello.kt

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

Compile the Kotlin code using the kotlin compiler.

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

Run the Kotlin code on jvm using the java command.

java -jar hello.jar

Create shortcut for compiling and running Kotlin program
Find out the Kotlin binary executables

which kotlin

Add the following to the ~/.bash_profile file, replace the bin path with yours using vim editor vim ~/.bash_profile

export PATH=$PATH:/Users/yourmacosusername/.sdkman/candidates/kotlin/current/bin/
function kotlinr() {
  echo Compiling, please wait...
  kotlinc $1 -include-runtime -d out.jar
  java -jar out.jar

Apply the updates in the ~/.bash_profile file.

source ~/.bash_profile 

Now you can compile and run your Kotlin programs at at the same time like this:

kotlinr hello.kt

Other Resources:

Search within Codexpedia

Custom Search

Search the entire web

Custom Search