Hello world -- top level function


How do I ensure that main is a top level function?

thufir@dur:~/kotlin$ kotlinc Hello.kt 
WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by com.intellij.util.text.StringFactory to constructor java.lang.String(char[],boolean)
WARNING: Please consider reporting this to the maintainers of com.intellij.util.text.StringFactory
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release
thufir@dur:~/kotlin$ kotlin Hello
error: 'main' method of class Hello is not static. Please ensure that 'main' is either a top level Kotlin function, a member function annotated with @JvmStatic, or a static Java method
thufir@dur:~/kotlin$ kotlin HelloKt
error: could not find or load main class HelloKt
thufir@dur:~/kotlin$ cat Hello.kt 
class Hello {

    public fun main(args: Array<String>) {
        println("Hello, world!" + args[0])

just want to run the hello world class.


In Kotlin the main function usually is a top-level function. See the online example.

I am not sure which name to use in this case. Simply try Hello and HelloKt like you did above.


This is a known issue when using kotlin with Java 9. See bug report KT-19051.