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

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.

主站蜘蛛池模板: 峨边| 莱芜市| 陆河县| 宜宾县| 锦州市| 南充市| 阜新| 沭阳县| 南投县| 建水县| 洪泽县| 丰顺县| 永登县| 安达市| 栾川县| 新密市| 杂多县| 林周县| 鹤岗市| 石渠县| 岫岩| 读书| 南华县| 康马县| 钦州市| 阳江市| 镇远县| 阳泉市| 麻江县| 宿松县| 大化| 潞城市| 克山县| 汉寿县| 朝阳县| 郎溪县| 鄯善县| 彝良县| 怀远县| 合水县| 新安县|