Kotlin – IntelliJ Project Setup

intellij-ideakotlin

I want to start a new project with Kotlin for the JVM using the IntelliJ IDE, but I can't get a configuration for it to work. I was attempting to follow this tutorial, and after that didn't work (the "Run '_DefaultPackage'" option never even showed up), I started trying to intuit what was supposed to be done without success.
What has happened so far (repeatedly):

  • I created a new project, selected "Kotlin – JVM" as the project type.
  • I clicked the "Create…" button for the Kotlin Runtime on the second page and selected "Copy to: lib".
  • I click "Finish" and the project created has one module with same name as my project. There is no default source file or any configuration.
  • I create a Kotlin file named "app.kt" (I've tried other names too, like "Main.kt"), and put the following source code in:
fun main(args: Array<String>){
    println("Hello world!")
}
  • I right clicked on the code editor AND the file in the left pane to find the "Run '_DefaultPackage'" option mentioned in the tutorial, but failed to find it in either.
  • I create a new Kotlin configuration, which asks that I put in a "Main class". Seeing this, I change the code to:
public class Main {
    fun main(args: Array<String>) {
        println("Hello world!")
    }
}
  • I edit my configuration and set the main class to "Main", and then run the configuration. It fails with this error: "Error running : Function 'main' not found in class 'Main'.

What am I missing?

Best Solution

You can't assemble the project cause main method is not static. So you should define it in companion object.

class HelloKotlin {

        companion object {
            @JvmStatic fun main(args: Array<String>) {
                println("Kotlin main is running here!")
            }
        }
    }
Related Question