简体   繁体   中英

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. I have configured:

  • Project Settings:
    • Project SDK: 11
    • Project language level: 8
  • Java Compiler Settings:
    • Use '--release' option for cross-compilation (Java 9 and later)
    • Project bytecode version: 8
  • Module Settings:
    • Project SDK (11)
    • Language level: Project default (8 - Lambdas, type annotations etc.)

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.

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

Is it possible to compile with JDK 11 and target Java 1.8 in IntelliJ?

There are other related questions which seem to work but not from IntelliJ:

Unfortunately, you can't use internal APIs if you compile with the --release N option. This is described in 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>.

And then follows the main paragraph:

For N < 9, the documented APIs consist of the that were on javac's default bootclasspath for JDK N. 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.

So, your question has nothing to do with IntelliJ IDEA. It's a limitation of the Java platform.

You have two ways:

  • Don't use internal APIs
  • Use JDK 8 for compilation

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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