简体   繁体   English

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

[英]How to compile with Java 11 target Java 1.8 in IntelliJ

I am trying to compile with JDK 11 but target Java 1.8 in IntelliJ. 我正在尝试使用JDK 11编译,但是在IntelliJ中使用Java 1.8。 I have configured: 我配置了:

  • Project Settings: 项目设置:
    • Project SDK: 11 项目SDK:11
    • Project language level: 8 项目语言水平:8
  • Java Compiler Settings: Java编译器设置:
    • Use '--release' option for cross-compilation (Java 9 and later) 使用'--release'选项进行交叉编译(Java 9及更高版本)
    • Project bytecode version: 8 项目字节码版本:8
  • Module Settings: 模块设置:
    • Project SDK (11) 项目SDK(11)
    • Language level: Project default (8 - Lambdas, type annotations etc.) 语言级别:项目默认值(8 - Lambdas,类型注释等)

I get a bunch of compilation errors saying that internal packages do not exist. 我收到一堆编译错误,说内部包不存在。 --add-exports is not valid with --target 1.8 so that doesn't help. --add-exports对于--target 1.8无效,因此无效。

The only thing that does work is to set the Project SDK back to Java 1.8 (which causes other problems when trying to run with Java 11 but that is another issue). 唯一可行的方法是将Project SDK设置回Java 1.8(当尝试使用Java 11运行时会导致其他问题,但这是另一个问题)。

Is it possible to compile with JDK 11 and target Java 1.8 in IntelliJ? 是否可以使用JDK 11编译并在IntelliJ中定位Java 1.8?

There are other related questions which seem to work but not from IntelliJ: 还有其他相关的问题似乎有效,但不是来自IntelliJ:

Unfortunately, you can't use internal APIs if you compile with the --release N option. 遗憾的是,如果使用--release N选项进行编译,则无法使用内部API。 This is described in JEP 247 : 这在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,它自动配置编译器以生成将链接到给定平台版本的实现的类文件。 --release N is roughly equivalent to: --release N大致相当于:

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

for N >= 9: -source N -target N --system <documented-APIs-from-N>. 对于N> = 9:-source N -target N --system <recorded-APIs-from-N>。

And then follows the main paragraph: 然后是主段落:

For N < 9, the documented APIs consist of the public APIs that were on javac's default bootclasspath for JDK N. 对于N <9,记录的API由javac的JDK N的默认bootclasspath上的公共 API组成。

I agree this is a big limitation actually. 我同意这实际上是一个很大的限制。 You can't even use sun.misc.Unsafe which has always been a critical part of OpenJDK. 你甚至不能使用sun.misc.Unsafe,它一直是OpenJDK的关键部分。

So, your question has nothing to do with IntelliJ IDEA. 所以,你的问题与IntelliJ IDEA无关。 It's a limitation of the Java platform. 这是Java平台的一个限制。

You have two ways: 你有两种方式:

  • Don't use internal APIs 不要使用内部API
  • Use JDK 8 for compilation 使用JDK 8进行编译

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM