1

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?

user118967
  • 4,895
  • 5
  • 33
  • 54
  • Should be fine, the version of eclipse you have is not aware of Java 11 (and eclipse uses its' own compiler); you could upgrade eclipse. – Elliott Frisch Mar 16 '19 at 07:37
  • in project facets change java 1.7 to 1.8 – Onkar Musale Mar 16 '19 at 07:37
  • It's prudent to build on jdk 8 if you intend to make your application compatible with java 8. This is because you can use java 11 features that are not available on java 8 (only to face errors such as nosuchmethoderror *at runtime*). Your config may cause the output class files to be compatible with java 8, but that doesn't mean that new features and APIs will be magically made possible on java 8. I've made this mistake myself in the past. Better build on java 8. – ernest_k Mar 16 '19 at 07:37
  • @ernest_k: "This is because you can use java 11 features that are not available on java 8 ". If my project's compliance is set to 1.8, then this won't happen, right? Eclipse will give me an error if I use anything above 1.8. – user118967 Mar 16 '19 at 07:50
  • @user118967 Wrong. Try calling an API method that was added only in java 11. I bet it will compile, but running that on java 8 will have a surprise for you... But there are tools to help prevent it, such as with maven. But then again, what's the point of compiling with java 11? Check ewramner's answer, it puts it best. – ernest_k Mar 16 '19 at 07:55
  • I tried, and it does *not* compile. I tried using "var i = 10" which is a Java 10 new feature, and Eclipse does not accept it because it is enforcing 1.8 compliance. The point of using Java 11 is that it is the currently supported version of OpenJDK. Also, I am avoiding the commercial license Oracle is requiring for its JREs. – user118967 Mar 16 '19 at 07:59
  • 1
    @user118967 I said API methods added in java 11. I know new language syntax won't compile, and that's not where the risk is. – ernest_k Mar 16 '19 at 08:20
  • @ernest_k It does not compile (e.g. `"".strip()`) when using the `--release` option that was introduced exactly for this. Eclipse supports this option since Photon (4.8) and `javac` since Java 9. See my answer below. – howlger Mar 17 '19 at 09:10

3 Answers3

5

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:

howlger
  • 31,050
  • 11
  • 59
  • 99
1

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.

ewramner
  • 5,810
  • 2
  • 17
  • 33
  • "you are probably compiling against the newer JDK classes and the API does change": if my project compliance is set to 1.8, then I should get an error if I try to use API versions above 1.8, correct? But I see your point that there may be incompatibility bugs. – user118967 Mar 16 '19 at 07:52
  • However, it is hard to believe that Eclipse would generate a warning because "it may have bugs". Warnings are based on formal specifications, so it seems there is an "official" reason besides the simpler practical possibility of bugs and trying to avoid them. – user118967 Mar 16 '19 at 07:56
  • No, this assumption is wrong. If you target 1.8 the compiler will generate code that can run on a 1.8 JVM, but it will compile with the API classes that it has. It does not have access to the API classes that were present in 1.8 if you only have 11 installed and there are changes. For example methods are added. The @since documentation is present in the JavaDoc for human readers, but the compiler makes no such checks and indeed cannot as the information is not recorded in the class files. – ewramner Mar 17 '19 at 06:51
  • 1
    @ewramner The compiler makes such checks when the `--release` option is used. Eclipse supports this option since Photon (4.8) and `javac` since Java 9. See my answer. – howlger Mar 17 '19 at 09:01
0

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.

neun24
  • 222
  • 2
  • 10