0

I have a few questions about the IntelliJ's initial setup for doing the most basic thing in Kotlin. Both are created by the same company, so I thought it would work seamlessly, but it did not.

  1. The new project option "Kotlin -> JVM | IDEA" (which I thought is for "Hello, world" type project) did not create any starting point file. It only created an empty "src" directory. All other project templates (other than explicit "Blank" solution) in all other IDE's included a starting point file and a starting method/class. Why does IntellJ have nothing?

  2. So, I created a new Kotlin class under the "src" directory. Searching Google "kotlin main" gave this page which had

    fun main(args: Array) { println("Hello World!") }

but with that, I could not select the main class in the "Kotlin" Run/Debug Configurations. After searching Google, I found that it should be:

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

It worked, but then, what is the one without @JvmStatic above? Is it wrong? Or is it correct? If it is another correct version, why doesn't the "Kotlin" Run/Debug Configurations recognise it?

  1. After changing it to the @JvmStatic main, the class automatically showed up in the "Search by Name" tab of the "Choose Main Class". But still, I could not select the class in the Project tab. The [OK] button gets disabled. Why is it so? A bug?

IntelliJ IDEA 2020.1.1 (Community Edition). All updated to the latest versions.

Damn Vegetables
  • 11,484
  • 13
  • 80
  • 135
  • 1
    Btw `fun main(args: Array) { println("Hello World!") }` is standard way to start an application/project, companion object one is discouraged. By default kotlin will make a class. if your file name is Myfile, then a corresponding class `MyfileKt` is going to be generated by compiler. – Animesh Sahu May 02 '20 at 11:38
  • And there must be a some problem with your ide, do a invalidate cache and restart, there should be main class option available in the run configuration, and should set itself up by default. You just need to run from main fun declairation -> `run` button, or by `ctrl+shift+f10` on main file – Animesh Sahu May 02 '20 at 11:42
  • @AnimeshSahu I have tried it on another computer with a newly-installed up-to-date IntellI, it was the same, empty `src` file. But the thing I have discovered is that if the `fun main` is outside of a class, then the KT gets automatically discovered. If it is inside a class, then not. Also, `(args:Array)` causes a compilation error of "One type argument expected for class Array". It worked after i changed it to "Array. – Damn Vegetables May 03 '20 at 12:26
  • sorry i just pasted the code from your code, `fun main() {}` when you never use args or `fun main(args: Array) {}` is actually the correct way, and yeah no classes/file is generated on starting a new project . – Animesh Sahu May 03 '20 at 13:17

0 Answers0