java 编译:源、目标和发布支持的版本 [英] java compilation : source, target and release supported versions

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

问题描述

我可能会为遗留项目做一些交叉编译,我注意到最近的 JDK 仅限于 sourcetarget 的某些特定版本释放 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 配置中的sourcetarget 版本不应优于使用的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 编译,但这并不意味着您可以选择任何支持的最新版本作为 source版本和 target 记录为支持的任何旧版本(见下文).
事实上,Java 版本可能会引入一些新特性,这些特性在编译时/运行时与旧 Java 版本不兼容.
例如,JDK 11 可以使用 8 作为 source 编译器版本编译类.也选择 8 作为 target 编译器版本,编译会通过.
但是,如果您改变主意并希望使用 11 作为 source 和 8 作为 target 进行编译,则编译将失败.
这并不总是明确记录在 javac 文档中.

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.

由于 sourcetarget 相同,release 参数应该优先于 source和<代码>目标.指定更短,它确保更好的交叉编译兼容性,即使您不进行交叉编译,也不会受到伤害.
更多解释请参考这个优秀的答案.

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 :

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).

  • 支持target :
  • 1.1, 1.2, 1.3, 1.4, 1.5 (also 5), 1.6 (also 6), and1.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

    • 支持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 :
    • 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 :

      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 :
      • 1.6(也是 6)、1.7(也是 7)、1.8(也是 8)和 9.

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

        • 支持发布:
        • 6、7、8 和 9.

          对于 Java 10

          • 支持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 :
          • 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.

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