Visual Studio Code Setup

Visual Studio Code (or just code / vscode) is an open source and free editor from Microsoft. It should not be confounded with Visual Studio which is a complete IDE. Vscode on the other hand is a simple editor but not and integrated development environment. Furthermore it does not need a license to run the program. It offers however a huge set of plugins for almost all programming languages. Some take aways are that it runs on almost all operating platforms. It offers built-in support for version control (GIT) and easy to use setup for Plugins. Since the programmer can use the same editor for several file formats, Visual Studio code has become quite popular within the programmer community. Especially for Android developer this is interesting, because you have to use several file types (e.g. XML, Kotlin, Java, …).

In this tutorial we will show how to run a simple Kotlin program in Vscode on a Windows operating system. Since Visual Studio Code is cross platform compliant the setup for Mac or Linux will be quite similar.

Download Visual Studio Code

First you need to download a version of Vscode. The installation is straight forward. Click on the following link and take the installer for your system.

https://code.visualstudio.com/download

During the installation you will be asked if you want to add “Open with Code” to the context menu of the windows explorer. I strongly recommend this option because it allows you quickly open folders and files in code by just right-clicking on them.

Install Kotlin – Language Plugin

In order to run Kotlin in Visual Studio Code you have to install 2 Plugins from the Marketplace. The first is the general language support. It comes with autocomplete, smart linting, additional tips and syntax highlighting. The second extension is Code runner, which is able to execute code in several languages. Code Runner comes with an API which allows to specify Kotlin as language which will run the code. 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 Visual Studio Code Plugin
Code Runner Visual Studio Code Plugin

If you’re interested what these extensions do, often their source code is listed in GitHub.

Adding Kotlin and Java to PATH

It is important to add both Kotlin and Java to the system (or user) path variables. Doing so will enable to run Kotlin and Java from the command line.

In windows open within the control panel the system properties and select advanced.

Windows System Properties

From there select the environment variables and change either the user or system PATH variable. In our case we added the following entries:

  • C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2019.1.3\plugins\Kotlin\kotlinc\bin
  • C:\Program Files\Java\jdk-12.0.1\bin

Set Kotlin and Java as local Path

If you don’t want to change the PATH variable or you’re not allowed to do so (e.g. you don’t have admin rights) you still can specify the paths within Visual Studio Code. To specify the path of language executables you need to add an executor map and set there the folders manually in the corresponding json file.

{
    "code-runner.executorMap": {
        "javascript": "node",
        "php": "C:\php\php.exe",
        "python": "python",
        "perl": "perl",
        "ruby": "C:\Ruby23-x64\bin\ruby.exe",
        "go": "go run",
        "html": "\"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe\"",
        "java": "cd $dir && javac $fileName && java $fileNameWithoutExt",
        "c": "cd $dir && gcc $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt"
    }
}

Running Hello World from Visual Studio Code

In our example we will just run a simple Hello World program on Vscode. For that we creat a fresh folder with a main.kt file containing a hello-world main function.

fun main() {
    println("Hello World!")
}

If you’re creating this file within Visual Studio Code, you will notice the code completion which is coming from the Kotlin Language plugin. This file is already associated with the Kotlin language.

By right-clicking on the main function a context menu appears. From there you can select the statement “run code“. The result is that your code is compiled and afterwards directly executed. You will notice a main.jar file next to your main.kt file.

A bit more complicated

You also can run some more complicated things like functions and classes. The code runner plugin can execute this file easily as long as they are in the same file.

class TestClass{
    fun print(){
        println("TestClass function")
    }
}

fun main() {
    println("Hello World!")
    var obj = TestClass()
    obj.print()
}

Vscode vs Intellij

Often the question comes which editor to use. In this case Vscode vs Intellij. In 90 % of the times Intellij is recommended to use. It comes from the creators of Kotlin and is way better equipped for this language. On the other hand Vscode is more general when it comes to different files. If you only need to change quickly a file and don’t need to do heavy refactorings then Vscode is fine and maybe the better choice. For real Kotlin or android development however it is better to use the appropriate editor like Intellij.

Conclusion

We have shown how to install and setup Visual Studio Code to run simple Kotlin programs. It is easy and straight forward to execute small programs as long as they are in the same file.
At the current stage it is not possible to run code which is distributed in several files. Probably it is possible to improve this but you have to change some settings how code runner starts the compiling process.

Since Vscode is one of the main editors of programmers it is a pro to be able to open Kotlin files in your project and to edit them with the help of code completion plugins. The draw back however is that you need a proper IDE (e.g. Intellij) to compile your program.