Java 9编译器中的--release标志是什么? [英] What is the --release flag in the Java 9 compiler?

查看:152
本文介绍了Java 9编译器中的--release标志是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Java 9的 javac 有一个新标志 - 发布

Java 9's javac has a new flag --release:

> javac --help
...

--release <release>
    Compile for a specific VM version. Supported targets: 6, 7, 8, 9

有什么不同? -source -target 标志?它只是 -source X -target X 的快捷方式?

How is it different from -source and -target flags? Is it just a shortcut for -source X -target X?

推荐答案

不完全是。

JEP 247:为旧平台编译版本 定义了这个新的命令行选项, - release


我们定义了一个新的命令行选项 - release ,它自动配置编译器以生成将链接到给定实现的类文件平台版本。对于 javac 中预定义的平台, - 版本N 相当于 - source N -target N -bootclasspath< bootclasspath-from-N> 。 (强调我的)

We defined a new command-line option, --release, which automatically configures the compiler to produce class files that will link against an implementation of the given platform version. For the platforms predefined in javac, --release N is equivalent to -source N -target N -bootclasspath <bootclasspath-from-N>. (emphasis mine)

所以不,它不等于 -source N -target N 。添加的原因在动机部分中说明:

So no, it is not equivalent to -source N -target N. The reason for this addition is stated in the "Motivation" section:


javac 提供两个命令行选项, -source -target ,可用于选择Java语言的版本分别由编译器和它生成的类文件的版本接受。但是,默认情况下, javac 将针对最新版本的平台API进行编译。因此,编译的程序可能会意外地使用仅在当前版本的平台中可用的API。无论传递给 -source 和`-target的值如何,此类程序都无法在旧版本的平台上运行。选项。这是一个长期的可用性痛点,因为用户希望通过使用这些选项,他们将获得可以在指定平台版本上运行的类文件。

javac provides two command line options, -source and -target, which can be used to select the version of the Java language accepted by the compiler and the version of the class files it produces, respectively. By default, however, javac compiles against the most-recent version of the platform APIs. The compiled program can therefore accidentally use APIs only available in the current version of the platform. Such programs cannot run on older versions of the platform, regardless of the values passed to the -source and `-target. options. This is a long-term usability pain point, since users expect that by using these options they'll get class files that can run on the specified platform version.

简而言之,指定源选项和目标选项不足以进行交叉编译。因为 javac ,默认情况下,针对最新的平台API进行编译,不能保证它们在旧版本上运行。您还需要指定与旧版本对应的 -bootclasspath 选项以正确交叉编译。这将包括要编译的正确API版本并允许在旧版本上执行。由于它经常被遗忘,因此决定添加一个命令行选项,它可以完成所有必要的操作以正确交叉编译。

In short, specifying the source and target options are not sufficient for cross-compilation. Because javac, by default, compiles against the most recent of the platform APIs, they can't be guaranteed to run on older versions. You also need to specify the -bootclasspath option corresponding to the older version to cross-compile correctly. This would include the correct API version to compile against and allow for execution on older version. Since it was very often forgotten, it was decided to add one command line option which did all the necessary things to correctly cross-compile.

进一步阅读在邮件列表 Oracle Docs 。最初的错误是在这里提交的。请注意,由于此选项的集成,JDK构建已捆绑了旧版本的平台API的描述,在风险和假设部分中提到。这意味着您不需要在计算机上安装旧版本以进行交叉编译。

Further reading in the mailing list and Oracle Docs. The original bug was filed here. Note that since the integration of this option, JDK builds have come bundled with descriptions of the platform APIs of older releases, mentioned under section "Risks and Assumptions". That means you don't need the older version installed on your machine for cross-compilation to work.

这篇关于Java 9编译器中的--release标志是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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