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

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

问题描述

Java 9 的 javac 有一个新标志 --release:

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 中预定义的平台,--release N 等价于 -source N -target N -bootclasspath ;.(强调我的)

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 文档.原始错误已在此处提交.请注意,由于此选项的集成,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天全站免登陆