https://kotlinlang.org logo
#getting-started
Title
# getting-started
p

Peter

03/25/2023, 2:43 PM
...and I have another question: As this is a pretty busy channel, I will open a new thread 😉
I have a hard time understanding the different ways how to call main functions of Kotlin code: a) if I have a global
fun main(args: Array<String>)
then
kotlinc Main.kt include-runtime -d Main.jar
will produce the
MainKt.class
, and the
Main.jar
. So
kotlin MainKt
or
java -jar Main.jar
need to be called. Why
MainKt
in the Kotlin case? The
Kt
suffix is normally only produced for Java? b) if the main function lives inside an
object
, that function needs to be annotated w/
@JvmStatic
, but then it can be called w/
kotlin Main
without th
Kt
suffix (as a
MainKt.class
does not exist). This still is not sufficient for Java (albeit the annotation), as the runtime dependency needs to be added again.
This is pretty asymmetrical, IMHO...
v

Vampire

03/25/2023, 2:49 PM
Where do you see any asymmetry? Top-level things in a
Foo.kt
file are not in any class, so an artifical class called
FooKt
is generated and the function put there. If it is inside a class, it is an instance function of that class unless you define it as
@JvmStatic
which makes it a static function of that class. The Kotlin runtime is of course always needed to run a Kotlin program with the Java interpreter just like any other dependency. The
--include-runtime
just builds a bad-practice fat-jar with the Kotlin runtime included, so that you do not have to add it to the classpath manually.
p

Peter

03/25/2023, 4:31 PM
Hey @Vampire Thank you again for your support! If the code is only run by Kotlin, why the hassle generating a *Kt named bytecode file?
That's the asymmetry I mean; ...
And how would the classpath look like, without packaging the stuff into a JAR?
v

Vampire

03/25/2023, 5:15 PM
Hey @Vampire Thank you again for your support! If the code is only run by Kotlin, why the hassle generating a *Kt named bytecode file?
Who says it is only run by Kotlin? If you run with
java -jar ...
it is still run by Java. Or you could still use it as library in some other Java project. ...
And how would the classpath look like, without packaging the stuff into a JAR?
Depends on which of the runtime libraries you use. That
-include-runtime
just packs everything in afair, even things you don't use or need. I'd usually simply use a proper build tool, especially Gradle, and let it build a proper distribution, e.g. using the
application
plugin in Gradle.
👍 1
3 Views