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
Regards,
CodePudding user response:
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!")
}
}
}
CodePudding user response:
In Kotlin, your main function goes outside any class definition.