Kotlin – Intellij Idea not building when I press Run

intellij-ideakotlin

I have a simple hello world project in Kotlin, running in Intellij Idea. I can build and run it just fine, but if I make changes, save and press the green run button (or Shift+F10), Intellij runs the old version of the file, before I made the changes. I can fix this by first Building the project (Ctrl+F9), then running it (Shift+F10). Why is this? Must I always build first, then run? Why doesn't Run build the project for me?

Here is my code, though I'm sure that's not the problem:

fun main(args: Array<String>) {
    println("hello world")
}

Best Answer

Make sure that the source directory is correctly configured in the project and that your code is actually in the source directory. Sometimes people import projects or there are some nuiances that cause you to have the code but the directory (src/main/kotlin for example) is not setup as a source code folder.

Is it a maven or gradle project? Or something else? Intellij has auto-detection for maven/gradle to setup the project but sometimes it must manually be triggered to do so if there have been changes made after the project was created or there is a pathing issue between intellij and the executables.

What did the "events" tab say or the output of the run screen?

Odds are good that intellij just isn't correctly configured to know about the project. It might be treating it like a generic project instead of a gradle/maven project for example. Or a java project that has no source code directory defined yet.

Goto the File --> Project Structure Settings --> Modules

And make sure that the "Mark As" for "sources" are correct.

Related Topic