How to run Kotlin code in Google Colab

Andres Sandoval
2 min readOct 25, 2024

Steps to Run Kotlin Code in Colab

  1. Install Kotlin Compiler: In Google Colab, you can install the Kotlin compiler using the following command:
!apt install -y kotlin

2. Write Kotlin Code in a .kt File: You can write your Kotlin code in a file, then compile and run it using the Kotlin compiler.

%%writefile hello.kt
fun main() {
println("Hello from Kotlin in Google Colab!")
}

3. Compile and Run Kotlin Code: After saving the Kotlin script, you can compile and run it using the following commands:

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

Explanation:

  • !apt install -y kotlin installs the Kotlin compiler on your Colab instance.
  • %%writefile hello.kt creates a Kotlin script.
  • !kotlinc hello.kt -include-runtime -d hello.jar compiles the Kotlin file into a JAR.
  • !java -jar hello.jar runs the compiled JAR.

Second way to run Kotlin method

%%capture
!wget -c https://github.com/JetBrains/kotlin/releases/download/v2.0.21/kotlin-compiler-2.0.21.zip; unzip kotlin-compiler-2.0.21.zip

--

--

No responses yet