Install

Kotlin to java online converter: https://www.codeconvert.ai/kotlin-to-java-converter.

See https://kotlinlang.org/docs/command-line.html#run-scripts

wget https://github.com/JetBrains/kotlin/releases/download/v1.7.22/kotlin-compiler-1.7.22.zip
cd $HOME/software
mkdir kotlin
cd kotlin
unzip /path/to/kotlin-compiler-1.7.22.zip
mv kotlinc 1.7.22
export PATH=$HOME/software/kotlin/1.7.22/bin:$PATH

Hello world

Create a file hello.kt:

./code/install/hello.kt
fun main() {
  println("hello world")
}

Usage 1

kotlinc hello.kt

It will generate a binary file HelloKt.class and a folder META-INF.

$ javap HelloKt.class

The above command prints the following:

Compiled from "hello.kt"
public final class HelloKt {
  public static final void main();
  public static void main(java.lang.String[]);
}

We can use:

kotlin HelloKt.class

to execute it.

Usage 2

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

# It can be run using:
java -jar hello.jar
kotlin -classpath ./hello.jar HelloKt

Note that hello.jar is actually a zip file.

kotlinc-jvm

To run kotlin inside an interactive shell, we can run the command:

kotlinc-jvm

or the command:

kotlinc

Naming convention

  • class: ClassName

  • function: funcName

  • variable: variableName

  • filename extension: kt