Java 9 编译器中的 --release 标志是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43102787/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the --release flag in the Java 9 compiler?
提问by ZhekaKozlov
Java 9's javac
has a new flag --release
:
Java 9javac
有一个新标志--release
:
> javac --help
...
--release <release>
Compile for a specific VM version. Supported targets: 6, 7, 8, 9
How is it different from -source
and -target
flags? Is it just a shortcut for -source X -target X
?
它与-source
和-target
标志有什么不同?它只是一个捷径-source X -target X
吗?
回答by Andrew Li
Not exactly.
不完全是。
JEP 247: Compile for Older Platform Versionsdefines this new command-line option, --release
:
JEP 247:为旧平台版本编译定义了这个新的命令行选项,--release
:
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 injavac
,--release N
is equivalent to-source N -target N -bootclasspath <bootclasspath-from-N>
. (emphasis mine)
我们定义了一个新的命令行选项,
--release
它会自动配置编译器以生成将链接到给定平台版本的实现的类文件。对于在 中预定义的平台javac
,--release N
相当于-source N -target N -bootclasspath <bootclasspath-from-N>
。(强调我的)
So no, it is not equivalent to -source N -target N
. The reason for this addition is stated in the "Motivation" section:
所以不,它不等同于-source N -target N
. 添加的原因在“动机”部分中说明:
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
提供了两个命令行选项,-source
和-target
,分别用于选择编译器接受的Java语言版本和它生成的类文件的版本。但是,默认情况下,javac
针对平台 API 的最新版本进行编译。因此,编译后的程序可能会意外使用仅在当前版本的平台中可用的 API。无论传递给-source
和的值如何,此类程序都无法在旧版本的平台上运行-target
。选项。这是一个长期的可用性痛点,因为用户期望通过使用这些选项,他们将获得可以在指定平台版本上运行的类文件。
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.
简而言之,指定源和目标选项不足以进行交叉编译。因为javac
,默认情况下,针对最新的平台 API 进行编译,所以不能保证它们可以在旧版本上运行。您还需要指定与-bootclasspath
旧版本对应的选项才能正确交叉编译。这将包括正确的 API 版本进行编译并允许在旧版本上执行。由于它经常被遗忘,因此决定添加一个命令行选项,该选项可以完成正确交叉编译所需的所有操作。
Further reading in the mailing listand 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.
在邮件列表和Oracle 文档中进一步阅读。最初的错误是在这里提交的。请注意,由于此选项的集成,JDK 构建与旧版本的平台 API 的描述捆绑在一起,在“风险和假设”部分中提到。这意味着您不需要在您的机器上安装旧版本来进行交叉编译。
回答by ZhekaKozlov
--release X
is more than just a shortcut to -source X -target X
because -source
and -target
are not sufficient to safely compile to an older release. You also need to set a -bootclasspath
flag which must correspond to the older release (and this flag is often forgotten). So, in Java 9 they made a single --release
flag which is a replacement for three flags: -source
, -target
and -bootclasspath
.
--release X
不仅仅是-source X -target X
因为-source
并且-target
不足以安全地编译到旧版本的快捷方式。您还需要设置一个-bootclasspath
必须对应于旧版本的标志(这个标志经常被遗忘)。因此,在 Java 9 中,他们制作了一个--release
标志来替代三个标志:-source
,-target
和-bootclasspath
。
So, this is an example of compiling to Java 1.7:
因此,这是编译为 Java 1.7 的示例:
javac --release 7 <source files>
Note that you don't even need to have JDK 7 installed on your computer. JDK 9 already contains the needed information to prevent you from accidental linking to symbols that did not exist in JDK 7.
请注意,您甚至不需要在计算机上安装 JDK 7。JDK 9 已经包含了防止您意外链接到 JDK 7 中不存在的符号所需的信息。