Java编译:源,目标和发行版本 [英] java compilation : source, target and release supported versions

查看:124
本文介绍了Java编译:源,目标和发行版本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可能会对旧项目进行交叉编译,并且我注意到最近的JDK对来源限于某些特定版本target release JVM参数。

如何获取这些参数的受支持版本?

I may do some cross-compilations for legacy projects and I noticed with recent JDKs that we are limited to some specific versions for the source, target and release JVM arguments.
How to get the supported versions for these arguments ?

推荐答案

确实支持的值取决于所使用的主要JDK版本。

您可以找到信息在相应的主要JDK版本的 javac 文档中(以下引用了链接)。

Indeed the supported values depend on the major JDK version used.
You can find the information on the javac documentation of the respective major JDK versions (the links are referenced below).

关于这些参数的一些一般说明:

Some general notes about these arguments :


  • 目标 Maven配置中的版本不应优于所使用的JDK版本。

    较旧的JDK版本无法编译,因为它不了解其规范。

  • The source and the target version in the Maven configuration should not be superior to the JDK version used.
    A older version of the JDK cannot compile with a more recent version since it doesn't know its specification.

提防:尽管最近的JDK可以接受较旧的Java版本作为 source 编译,这表示您可以选择任何受支持的最新版本作为,并选择记录为受支持的任何较早版本作为 target (见下文)。

实际上,Java版本可能会引入并非旨在兼容的新功能。

例如,JDK 11可以将具有8的类作为 source 编译器版本进行编译。通过同时选择8作为 target 编译器版本,编译将通过。

但是,如果您改变主意并且希望使用11作为 source 和8作为 target ,编译将失败。

并非总是明确记录在此

Beware : while a recent JDK can accept as source compilation an older Java version, it doesn't mean that you can select as source any supported recent version and as target any older version that is documented as supported (see below).
In indeed, Java versions may introduce new features that were not designed to be compatible with older Java version at compile time/run time.
For example a JDK 11 can compile classes with 8 as source compiler version. By choosing also 8 as target compiler version, compilation will pass.
But if you change your mind and that you want to compile with 11 as source and 8 as target, compilation will fail.
That is not always explicitly documented in the javac documentation.

Java 9中存在 release 参数。

The release argument exists from Java 9.

作为来源目标相同,应该优先使用 release 参数而不是 source target 。指定的时间较短,可以确保更好的交叉编译兼容性,即使您不进行交叉编译,也不会造成损害。

有关更多说明,请参见这个出色的答案

As the source and the target are the same, the release argument should be favored over source and target. It is shorter to specify and it ensures a better cross compilation compatibility and whatever even if you don't do cross compilations, it will not hurt.
For more explanations please refer to this excellent answer.

源/目标/发行版支持的版本:

> 对于Java 7


  • 支持的 source

  • supported source :

1.3、1.4、1.5(也为5),1.6(也为6)和1.7(也是7)。

1.3, 1.4, 1.5 (also 5), 1.6 (also 6), and 1.7 (also 7).



  • 支持的目标

    • supported target :

    • 1.1、1.2、1.3、1.4、1.5(也为5),1.6(也为6)和
      1.7(也是7)。

      1.1, 1.2, 1.3, 1.4, 1.5 (also 5), 1.6 (also 6), and 1.7 (also 7).

      对于Java 8


      • 支持的

      • supported source :

      1.3、1.4、1.5(也为5),1.6(也为6),1.7(也为7)和1.8(也是8)。

      1.3, 1.4, 1.5 (also 5), 1.6 (also 6), 1.7 (also 7), and 1.8 (also 8).



      • 支持的 target

        • supported target :

        • 1.1,1.2,1.3,1.4,1.5(也5),1.6(也6),1.7
          (同样是7)和1.8(也是8)。

          1.1, 1.2, 1.3, 1.4, 1.5 (also 5), 1.6 (also 6), 1.7 (also 7), and 1.8 (also 8).

          对于Java 9


          • 支持的 source

          • supported source :

          1.6(也是6),1.7(也是7), 1.8(也是8)和9。

          1.6 (also 6), 1.7 (also 7), 1.8 (also 8), and 9.



          • 支持的 target

            • supported target :

            • 1.6(也是6),1.7(也是7),1.8(也是8)和9。

              1.6 (also 6), 1.7 (also 7), 1.8 (also 8) and 9.



              • 支持的版本

                • supported release :

                • 6、7 8和9。

                  6, 7, 8, and 9.

                  对于Java 10


                  • 支持的

                  • supported source :

                  1.6(也为6),1.7(也为7),1.8(也为8), 9和10。

                  1.6 (also 6), 1.7 (also 7), 1.8 (also 8), 9, and 10.



                  • 支持的 target

                    • supported target :

                    • 1.6(也是6),1.7(也是7),1.8(也是8),9和10。

                      1.6 (also 6), 1.7 (also 7), 1.8 (also 8), 9 and 10.



                      • 支持的版本

                        • supported release :

                        • 6、7、8、9和10。

                          6, 7, 8, 9, and 10.

                          对于Java 11


                          • 支持的源/目标/版本

                          • supported source/target/release :

                          6、7、8、9、10和11。

                          6, 7, 8, 9, 10, and 11.

                          你可以有关详细信息,请参见 JDK 1.8的javac文档 JDK 9 JDK 10 JDK 11

                          You could have more details in the javac documentation for JDK 1.8, JDK 9, JDK 10 and JDK 11

                          这篇关于Java编译:源,目标和发行版本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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