Running a Kotlin program from the command line?


#1

I've been trying to run a Kotlin JVM program (not script) from the command line without success. The program is compiled in IDEA (and packaged into a jar) and runs fine through the IDEA run interface.

Should I be using java directly, or kotlinc-jvm to do this? When I try to use java on the jar it complains about not having a main class. When I try to run kotlinc-jvm on the jar, it simply opens the interactive shell. It sort of works when I try to interpret my main.kt file as a script with kotlinc-jvm, but the compilation fails since none of the proper dependencies have been specified.

Does anyone have some experience running programs on the command line? Any insight would be helpful.


#2

I think the main class property in the manifest file is wrong. The main class is always <packagename>.namespace or just namespace if you don't have a package.


#3

Maybe this old post might help

http://devnet.jetbrains.net/message/5471284


#4

Danial was correct: the main class was not properly set in the generated jar. Once I set the main class to the namespace class, it works exept that all of the depdendencies still need to be manually placed into the classpath.

To get my final solution, I ended up making a bash script that searches my project’s lib folder for all dependent jars and creates a classpath dynamically. The result is something like this:

java -classpath <all dependent jars>:library.jar libary.namespace

This works, but it seems a bit hacky. To be honest I'm more used to the .NET world where dependencies are resolved at the assembly (i.e. jar) level and dependent assemblies can simply be placed next to the target assembly at runtime. I'm sure that someone who's much more clever at Ant could make a target to generate this classpath automatically, but it's a bit beyond me at this point.