0

enter image description here

I am constantly getting the error (bottom right), but I have configured everything to run Kotlin/Java together. So, where is the missing bit which I cannot see.

UPDATE

A comment indicated that my pkg name was wrong, but it didn't matter regardless, as the below updated screenshot shows

enter image description here

Regards,

3
  • your package signature is wrong Commented Jun 3, 2022 at 22:29
  • @IłyaBursov yes that was typo, but I edited the file, restarted, and yet - same thing. Check the updated image Commented Jun 3, 2022 at 22:39
  • compare your code with sample from play.kotlinlang.org/byExample/01_introduction/01_Hello%20world Commented Jun 3, 2022 at 23:01

2 Answers 2

2

If you want to write the main function without any parameters, it has to be outside of the class like this:

package org.example

fun main() {
    println("Hello World!")
}

Note that in this case, the main function will be inside an artificial implicit class that is called like the containing file suffixed with Kt. This means, that in your example where you place the main function inside a file AppKt.kt, the resulting main class is called AppKtKt.

If you want to have the main function inside a class, you have to specify a parameter for the arguments. Also, it needs to be static, i.e. it has to be inside an object and must be annotated with @JvmStatic. This can be done by declaring an object:

object AppKt {
        @JvmStatic
        fun main(args: Array<String>) {
            println("Hello World!")
        }
}

Or you can put the main function inside the companion object of a class:

class AppKt {
    companion object {
        @JvmStatic
        fun main(args: Array<String>) {
            println("Hello World!")
        }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

In Kotlin, your main function goes outside any class definition.

2 Comments

It can, but it doesn't have to (iirc).
@Slaw only if you put it in a singleton object and mark it @JvmStatic.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.