为什么Kotlin编译后需要捆绑其运行时? [英] Why Kotlin needs to bundle its runtime after compiled?

查看:183
本文介绍了为什么Kotlin编译后需要捆绑其运行时?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是想了解基础架构,我认为我错了.

I'm just trying to understand the underlying architecture, which I think I am getting wrong.

以教程此处为例.

当我这样做时:

kotlinc-jvm hello.kt -include-runtime -d hello.jar

如果编译器已将代码转换为Java字节码,为什么需要将Kotlin运行时捆绑到jar中?

Why it's needed to bundle the Kotlin runtime into the jar if the compiler already converted the code to Java bytecode?

推荐答案

使用Java编写应用程序时,您将依赖于所有标准类库.每个JRE都包含java.类(例如java.lang.*java.util.* ...),因此您无需自己打包它们.

When you write an application in Java, you get to rely on all of the standard class libraries. The java. classes (e.g. java.lang.*, java.util.* ...) are included with every JRE, so you don't need to package them yourself.

Kotlin包括自己的标准类库( Kotlin运行时),与Java类库分开.要分发一个jar文件,任何拥有普通JRE的人都可以运行该jar文件,您还需要捆绑Kotlin运行时.

Kotlin includes its own standard class library (the Kotlin runtime), separate to the Java class library. To distribute a jar file that can be run by anyone with a plain old JRE, you need to bundle the Kotlin runtime as well.

如果您没有捆绑Kotlin运行时,那么用户将必须确保在执行应用程序时Kotlin运行时在类路径中.您链接的页面提供了这种情况的示例:

If you didn't bundle the Kotlin runtime, then your user would have to ensure the Kotlin runtime was on the classpath when executing your application. The page you linked gives an example of this scenario:

编译库

如果您要开发供其他Kotlin应用程序使用的库,则可以生成.jar文件,而无需将Kotlin运行时包含在其中.

If you’re developing a library to be used by other Kotlin applications, you can produce the .jar file without including the Kotlin runtime into it.

$ kotlinc-jvm hello.kt -d hello.jar

如果您以其他Kotlin用户为目标,那么可以合理地假设他们已经可以使用Kotlin运行时.但是,如果您要为最终用户部署应用程序,则要包括Kotlin运行时,以便您的应用程序是独立的.

If you're targeting other Kotlin users, then its reasonable to assume they'll already have the Kotlin runtime available to them. However, if you're trying to deploy an application for an end-user, then you want to include the Kotlin runtime so that your application is self-contained.

这篇关于为什么Kotlin编译后需要捆绑其运行时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆