intellij-ideakotlin

How to run Kotlin function in Intellij Idea


I have created a test project aiming to start with Kotlin in IntelliJ IDEA CE.

The IDE version and the system is 2017.1.4 Build #IC-171.4694.23, built on June 6, 2017 JRE: 1.8.0_112-release-736-b21 x86_64 JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o Mac OS X 10.12.4

There is one .kt file with a main function in it. When I right-click on it I can select Run, but I get an error form JUnit: !!! JUnit version 3.8 or later expected.

I figured out it was about missing configuration, but I can't set up a proper one for Kotlin as well. Namely it requires a 'main class', which I don't have. Setting package name instead, as some other discussions suggest didn't help either.

What is the proper configuration for Kotlin in IDEA?

Thanks!


Solution

  • 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