Kotlin has been on my mind. The back of my mind, but still. And apart from a very short workshop last year at Devoxx I have not really tried it out. VS Code is my preferred IDE nowadays. So I wanted to get going with Kotlin in VS Code. Of course, plenty of resources are available to help me out. And in just under 20 minutes, I was good to go and already going. Now I need to learn a little bit more about Kotlin, to do meaningful things. But the environment is there.
By the way: that little Code Runner extension for VS Code is very nice. It can easily run snippets of code in many languages.
Install VS Code and install Java (JDK) – follow for example the steps in this article: https://technology.amis.nl/2020/09/20/my-steps-for-getting-started-with-java-development-on-windows/
Install Kotlin – download kotlin compiler zip for your platform from https://github.com/JetBrains/kotlin/releases/latest ; unzip the archive to any directory, for example c:\program files. Add the full path to kotlinc/bin to the PATH environment variable:
Restart your terminal program (to let the change to PATH take effect), and check that you can start the Kotlin CLI by saying kotlinc.
Try some simple interactive Kotlin statements, such as
As per the instruction in this article , let’s add two plugins to VS Code:
Code Runner – which can run almost any type of code snippet, including Kotlin. That is to say: Code Runner does not build the executable itself, but runs a command to compile it with the Kotlin compiler and runs it on the Java Virtual Machine)
Kotlin Language – this plugin provides syntax support for the Kotlin programming language and other basic features to help make writing Kotlin much more pleasant in VS Code
I have created a new file (main.kt) in a new folder (c:\research\kotlin-lab).
After pasting the following code to the file:
fun main() {
println(“Hello World!”)
}
I have selected the code, right clicked to bring up the context menu
and selected the option Run Code.
The code is compiled and executed, the result is showing on the command line:
Next, I create a new file called args.kts. This file is to contain a Kotlin script, with the following contents:
println(“Your arguments are:”)
for (i in 0 until args.size) {
println(“$i: ${args[i]}”)
}
This script can be executed on the command line using the following command:
kotlinc -script .\args.kts hello boo world
with the last three arguments non sensical values that you can replace with your own non sensical values.
Note: the code can also be executed using Code Runner – but then no arguments are passed to it and the result is even more lame:
Resources
Article on Kotlin extensions in VS Code: https://medium.com/@thunderz99/debugging-kotlin-program-using-vscode-318ed43fe2f0
Visual Studio Setup for Kotlin development: https://kotlin-code.com/ide/visual-studio-code/setup/