How to run a Kotlin.kt file from command line?

Could you please use three backticks (```) before and after code blocks to make code and console sessions more readable? Thanks.

You are compiling a Kotlin script (kts) instead of a Kotlin file (kt). Change the extension, and you will get a JAR containing class hello.HelloKt with a static main, and a manifest marking this class as the main class.

This does not mean that it will work using java -jar hello.jar. It does not work because the Kotlin run-time is not on the class path.

The easiest way is to use the kotlin command. This will add the Kotlin run-time to the class path:

kotlin -cp hello.jar hello.HelloKt

Note that kotlin hello.jar won’t work, because then the run-time is not added to the class path. I have no idea why it is not added in this case though.

If you want to run a Kotlin script directly, you have to put the body of main(...) at the top level, and use kotlinc. See Executing Kotlin scripts in command shell - #2 by udalov