How to run Kotlin function in Intellij Idea

Check that your IDEA is up to date. When creating a new Project you can select Kotlin like this:

Project creation with Kotlin

This will automatically configure Kotlin for you. If you started a Java project and want Kotlin support, there will be a little window in the bottom right corner to configure Kotlin in the project for you. (not needed if the project is created like shown above)

Kotlin configuration in Java project

There you want to select Java, not Javascript and after that click on "Configure '...' module in '...' project as Kotlin (Java) module..."

When your project is set up correctly, make sure that your main function is called "main" and has a parameter of type Array<String>.

fun main(args: Array<String>) {

}

Next to it a Kotlin "K" will appear, which you can click on to run your main function.

Main function execution


I had a problem where I could not even run the code, IntelliJ IDEA offered me to create a new configuration.

I solved the problem by marking the "src" folder as "Sources" in Project Structure (File > Project Structure, select src and mark it as Sources). For some reason generating new project didn't mark src folder as sources automatically.

Once I did that an arrow appeared next to the main function and I could run it.


Coming from a Java background, I instinctively put the main function inside a class. Then IntelliJ wouldn't let me run it. After taking the main function out of class to the top level, IntelliJ automatically put the green arrow next to the main function and added Run to the context menu (brought up by right clicking the file).