Kotlin ep.kt -run


#1

What is the point in making double work with:

kotlin ep.kt -include-runtime -d ep.jar
java -jar ep.jar 

Why not just make it a single command?

kotlin ep.kt -run

#2

If you have a single-file program that you want to execute quickly, you can save it to a .kts file, and compile and run it as “kotlinc -script foo.kts”


#3

It is not a script. I want finally to package it as a single file executable. Saving it to .kts doesn’t produce any output:

ep.kts:6:10: warning: parameter 'args' is never used
fun main(args: Array<String>) {
         ^

Because my code is in main and script mode doesn’t evaluate it.

And if I run it without renaming to .kts then:

exception: java.lang.RuntimeException: Failed to evaluate script: java.lang.IllegalStateException: Script must be parsed
        at org.jetbrains.kotlin.cli.jvm.compiler.KotlinToJVMBytecodeCompiler.compileScript(KotlinToJVMBytecodeCompiler.kt:526)
        at org.jetbrains.kotlin.cli.jvm.compiler.KotlinToJVMBytecodeCompiler.compileAndExecuteScript(KotlinToJVMBytecodeCompiler.kt:226)
        at org.jetbrains.kotlin.cli.jvm.K2JVMCompiler.doExecute(K2JVMCompiler.kt:175)
        at org.jetbrains.kotlin.cli.jvm.K2JVMCompiler.doExecute(K2JVMCompiler.kt:54)
        at org.jetbrains.kotlin.cli.common.CLICompiler.exec(CLICompiler.java:178)
        at org.jetbrains.kotlin.cli.common.CLICompiler.exec(CLICompiler.java:125)
        at org.jetbrains.kotlin.cli.common.CLICompiler.exec(CLICompiler.java:52)
        at org.jetbrains.kotlin.cli.common.CLICompiler.doMainNoExit(CLICompiler.java:379)
        at org.jetbrains.kotlin.cli.common.CLICompiler.doMain(CLICompiler.java:369)
        at org.jetbrains.kotlin.cli.jvm.K2JVMCompiler$Companion.main(K2JVMCompiler.kt:283)
        at org.jetbrains.kotlin.cli.jvm.K2JVMCompiler.main(K2JVMCompiler.kt)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
        at java.lang.reflect.Method.invoke(Unknown Source)
        at org.jetbrains.kotlin.preloading.Preloader.run(Preloader.java:73)
        at org.jetbrains.kotlin.preloading.Preloader.main(Preloader.java:35)
Caused by: java.lang.IllegalStateException: Script must be parsed
        at org.jetbrains.kotlin.cli.jvm.compiler.KotlinToJVMBytecodeCompiler.compileScript(KotlinToJVMBytecodeCompiler.kt:522)
        ... 16 more

I don’t understand why .kts scripts should not execute main() if it is present once parsing is over. I also don’t understand why a special extension rule is enforced when I want to run Kotlin source in -script mode.


#4

It seems that a bash function or alias would be your salvation. You could even call it kotlin and have it shadow the real interpreter (still accessible through it’s full path). Btw. the kotlin way is exactly the way that java does stuff as well except that the creation of the jar file is a bit trickier than the kotlin command.


#5

@pdvrieze I am not looking for a quick hack. I am interested in design decisions behind the behavior.