Search code examples
kotlinkotlin-script

Kotlin script (.kts) file -- no println?


I am experimenting with the use of Kotlin as a scripting language. According to their docs, you should be able to run top-level code in a Kotlin script.

A simple "Hello, World" program I wrote using their official example is not outputting any text. It compiles/interprets, terminates successfully, but it appears that the println() statement does nothing

fun main(args: Array<String>) {
    println("Hello, World!")
}

Does anyone know where I can find a table / summary of what is actually supported when using Kotlin as a scripting language? What am I missing in making it do a simple print statement.

I am running using a Kotlin SDK installed via sdkman on Ubuntu. Running from the vanilla terminal provided with Ubuntu. The expected output would be a line where "Hello, World!" is shown, but there is no output at all.


Solution

  • A function in it self does not get executed. Its a declaration like a variable. In a script it must be invoked.

    fun main() { // removed unused args
        println("Hello, World!")
    }
    
    // Add this
    main()