官术网_书友最值得收藏!

Kotlin runtime

When we compiled Hello World! and produced the JAR, we instructed the compiler to bundle in the Kotlin runtime. Why is the runtime needed? Take a closer look at the following bytecode that was generated, if you haven't already done so. To be more specific, look at line 3. It invokes a method to validate the fact that the args variable is not null; therefore, if you compile the code without asking for the runtime to be bundled in, and then try to run it, you will get an exception:

$ kotlinc HelloWorld.kt -d HelloWorld.jar
$ java -jar HelloWorld.jar
Exception in thread "main" java.lang.NoClassDefFoundError:  kotlin/jvm/internal/Intrinsics at HelloWorldKt.main(HelloWorld.kt)
Caused by: java.lang.ClassNotFoundException:  kotlin.jvm.internal.Intrinsics

The runtime footprint is very small; at approximately 800 K, you can't argue otherwise. Kotlin comes with its own standard class library (Kotlin runtime), which is different from the Java library. As a result, you need to merge it into the resulting JAR, or provide it in the classpath, as follows:

$ java -cp $KOTLIN_HOME/lib/kotlin-runtime.jar:HelloWorld.jar  HelloWorldKt

If you develop a library for the exclusive use of other Kotlin libraries or applications, then you don't have to include the runtime. Alternatively, there is a shorter path that involves passing a flag to the Kotlin compiler, as follows:

$ kotlinc -include-runtime HelloWorld.kt -d HelloWorld

The preceding code will include the runtime when assembling the final JAR file.

主站蜘蛛池模板: 广平县| 闻喜县| 邓州市| 布拖县| 阿图什市| 平定县| 手游| 金山区| 原平市| 香格里拉县| 西贡区| 文登市| 乌兰浩特市| 门头沟区| 定西市| 铅山县| 海兴县| 云林县| 磴口县| 赤水市| 闽侯县| 三台县| 朝阳市| 个旧市| 龙岩市| 通渭县| 邢台县| 阿尔山市| 池州市| 调兵山市| 婺源县| 灵山县| 原阳县| 工布江达县| 旌德县| 凤阳县| 长沙市| 噶尔县| 丰台区| 广元市| 龙海市|