Why does Eclipse complain that the project specifies compiler compliance 1.8 but a JRE 11 is used?

8.2k views Asked by At

I set an Eclipse project compiler compliance to 1.8 because I want to generate jar files that can be executed in computers with JRE 8 installs.

However, in my development computer I only have JRE 11 installed. I assumed this to be ok because I expected a JRE 11 to be able to execute 1.8 files and above.

However, Eclipse produces a warning about this (which I cannot find a way to disable), and the compiler preferences dialog says "When selecting 1.8 compliance, make sure to have a compatible JRE installed and activated (currently 11)", which makes is sound like a JRE 11 is not compatible with 1.8. Is that really the case?

What wrong assumptions I am making here, exactly?

Also, do I really need to have a JRE 8 installed in order to produce JRE 8-compatible code?

3

There are 3 answers

0
howlger On BEST ANSWER

In Project > Properties: Java Compiler check the Use '--release' option checkbox to avoid the warning.

With Use '--release' option and the compiler compliance level set to 1.8, for example " foo ".strip() is shown as an error even with a Java 11 JDK because String#strip() which was introduced in Java 11 is missing in Java 8.

enter image description here

See also:

0
neun24 On

Any chance you didn't install the JDK? If I remember correctly the warning was related to that. Sounds like my Eclipse, OpenJDK, Ubuntu combination missing the openjdk-11-jdk package.

4
ewramner On

If you want to be 100% sure, compile with the same JDK that you will use in production, at least with the same major version. JRE 11 can execute 1.8 class files for sure. An older JVM may not be able to run the newer class files, though. Even if you target an old version and produce class files that should be compatible you are probably compiling against the newer JDK classes and the API does change. That may break your code when you try to run it on an older JVM.

Most of the time it will work, but better safe than sorry! Listen to the warnings in this case, they do have a point.

EDIT: see answer by howlger instead for the --release option.