0

I have just installed Kotlin version 1.7.0 and the bin folder has the following runnables.

  • kotlinc
  • kotlin-jvm
  • kotlin

After quick check found that kotlinc and kotlin-jvm are used to compile code to bytecode just like javac in Java. Why there are 2 commands for kotlin compilation. Is there any significant difference?

And, should I use java command to run the bytecode or kotlin command to run the bytecode. Any tradeoffs in terms of performance.

srk
  • 4,857
  • 12
  • 65
  • 109

1 Answers1

1

The official docs don't say too much about those command-line tools. And while you can call them with -help, that merely describes the other options they accept.

However, I can tell you what happens on my system (macOS, where Kotlin is installed using HomeBrew). I don't know how much of the below applies to other platforms, but I suspect that at least Windows and Linux would be very similar.

Those executables are wrapper scripts, and so it's possible to see some of what they do. (They end up running Kotlin programs, which limits what you can see…) 

Compiling:

kotlinc-jvm is a very simple wrapper script which merely calls kotlinc — so there's clearly no effective difference between them!

That shows Kotlin/JVM is the default compilation target, at least on my system. (kotlinc-js also calls kotlinc, but only after setting the $KOTLIN_COMPILER environment variable, which changes the program that kotlinc ends up running.) Maybe kotlinc has a different default on other systems — or at least they wanted to allow for the possibility.

Running:

AIUI, running kotlin is effectively equivalent to running java, except that kotlin adds the Kotlin runtime classes to the classpath.

You can of course add them manually when running java (e.g. in the $CLASSPATH or in a -cp parameter); but you would then need to know where they are, or have some way to find them. (On my system they're in …/libexec/lib/. The only essential one is the relevant kotlin-stdlib jar, though some code also needs kotlin-reflect and maybe others too. Or if you have a full Kotlin installation, I think you can simply add kotlin-runner.jar, which pulls in the others for you.)

I don't think there's any other benefit to using kotlin.

Note that if you build a ‘fat’ .jar/.war file, which includes the Kotlin runtime libs, then most systems will use java to run that anyway.

gidds
  • 16,558
  • 2
  • 19
  • 26