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?
Open your file that contains your main function and go to menu->"Edit configurations" then select "+" in the dialog, "Application" as the type set the name to what you want and set the main class by clicking on the button next to the top entry box (the one labeled "main class").
The select "use class path of module" and select your module from the drop down box. Click "apply" and close the dialog. Now you should be able to run with shift+F10, debug with shift+F9 and edit run configurations with shift+alt+F10. You can also run or debug from the two buttons in the top right hand of your main screen.