- Learn Kotlin Programming(Second Edition)
- Stephen Samuel Stefan Bocutiu
- 228字
- 2021-06-24 14:13:23
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.
- 算法基礎:打開程序設計之門
- Learning Linux Binary Analysis
- HTML5+CSS3網站設計基礎教程
- Microsoft System Center Orchestrator 2012 R2 Essentials
- 小程序開發原理與實戰
- Node.js全程實例
- Django實戰:Python Web典型模塊與項目開發
- Learning Android Application Testing
- 交互式程序設計(第2版)
- 零基礎C#學習筆記
- 進入IT企業必讀的324個Java面試題
- 黑莓(BlackBerry)開發從入門到精通
- DB2SQL性能調優秘笈
- Mastering ASP.NET Web API
- C語言程序設計