Search code examples
kotlinintellij-idea

Intellij running multiple kotlin files inside project, instead the one selected


Learning Kotlin and IntelliJ,

Before yesterday if I wanted to run one single .kt file, it was all good. Many ways to do it. I was doing various exercises, all in the same file exercises.kt. After finishing a code, I would just transform it into a comment and start the next one in the same file.

After many exercises, I decided to create a .kt file for each one. But, now, when I run a single file for one exercise Intellij is running ALL files inside the project, instead of just the one I selected.

EDIT: Every .kt file got its own main function, some with a class. To run a single file I used different methods. Clicking the play button at the left of the main function, or clicking the play button at the top bar with the current file selected and clicling with the right button on top of the file in the project list and selecting "Run filekt" (Same as Ctrt + shift + F10). Like I said before the change explained it was all good.

Here it is my run configuration:

Run Configuration

Content of one of my files that was single running before I changed

fun main() {
    val amanda = Person("Amanda", 33, "play tennis", null)
    val atiqah = Person("Atiqah", 28, "climb", amanda)

    amanda.showProfile()
    atiqah.showProfile()
}

class Person(val name: String, val age: Int, val hobby: String?, val referrer: Person?) {
    fun showProfile() {
        when {
            referrer == null -> println("""Name: ${name}
Age: ${age}
Likes to ${hobby}. Doesn't have a referrer."""")
            else -> println("""Name: ${name}
Age: ${age}
Likes to ${hobby}. Has a referrer named ${referrer.name}, who likes to ${referrer.hobby}""")
        }
    }
}

When Intellij run all the files, this is showing in the project errors. This is new as well. If I try to run the SongCatalog files on Kotlin Playground it's all good.(The Special Auction is in progress)

ProjectError

For the rest of the files, here's the repository: Google Learning Repo


Solution

  • IntelliJ does not run all the files. It compiles all the files, but only runs the file that you specified in the run configuration.

    However, since two of your files, SongCatalog.kt and SongCatalog2.kt, both declare a SongCatalog class, there is a compiler error. The same class cannot be declared twice, after all. Even if you are just trying to run some other file, all the files in your project has to be compiled.

    Using Packages

    I recommend just putting each exercise that you are doing in its package. This way the two SongCatalogs are distinct. For example, at the start of SongCatalog.kt, you can write:

    package exercise4_1
    

    and at the start of SongCatalog2.kt, you can write:

    package exercise4_2
    

    Put each file into a folder with the same name as the package. Do this for all the other files containing your exercises so that it's organised.

    Make sure that IntelliJ don't automatically insert imports, importing things from other exercises, causing conflicts again.

    Then in your run configuration, write add the package name as a prefix for the main class. For example, to run SongCatalog.kt, you'd write exercise4_1.SongCatalogKt.


    Using Modules

    There is also another rather overkill method of having IntelliJ only compile a part of your project's files. You can put each of your exercises' files into a different module.

    To add a module, go to File -> New -> Module. Then you can give that module a name and add new .kt files to the /src folder of that module.

    enter image description here

    enter image description here

    Configure your run configuration the same way as before, except choose the module that you want to run in the "use class path of module" dropdown.