如何在 IntelliJ 中使用 Java 11 目标 Java 1.8 进行编译

How to compile with Java 11 target Java 1.8 in IntelliJ

我正在尝试使用 JDK 11 进行编译,但在 IntelliJ 中以 Java 1.8 为目标。我已经配置:

我收到一堆编译错误,说内部包不存在。 --add-exports--target 1.8 无效,所以这无济于事。

唯一可行的方法是将 Project SDK 设置回 Java 1.8(这在尝试 运行 和 Java 11 时会导致其他问题,但这是另一个问题).

是否可以在 IntelliJ 中使用 JDK 11 和目标 Java 1.8 进行编译?

还有其他相关问题似乎有效但不是来自 IntelliJ:

遗憾的是,如果使用 --release N 选项进行编译,则无法使用内部 API。 JEP 247:

中对此进行了描述

A new command-line option, --release, is defined, which automatically configures the compiler to produce class files that will link against an implementation of the given platform version. --release N is roughly equivalent to:

for N < 9: -source N -target N -bootclasspath <documented-APIs-from-N>,

for N >= 9: -source N -target N --system <documented-APIs-from-N>.

然后是正文:

For N < 9, the documented APIs consist of the public APIs that were on javac's default bootclasspath for JDK N.

我同意这实际上是一个很大的限制。您甚至不能使用一直是 OpenJDK.

的关键部分的 sun.misc.Unsafe

因此,您的问题与 IntelliJ IDEA 无关。这是 Java 平台的限制。

你有两种方法:

  • 不要使用内部 API
  • 编译使用JDK8