56

For some time I tried to understand, but I still don't get exactly what "compiler compliance level" for a project in Eclipse means. I looked all over this site and Google and couldn't find an answer I could understand.

Let's say I want my program to be able to run on JRE 6.

I can do: Project > Preferences > Java Build Path > Libraries, and set the JRE library I use to JRE 6.

Why isn't this enough?

I never understood why I also need to set the compiler compliance setting to JRE 6.

I'd like to understand the difference between using JRE 6 in a project, and setting the project's compiler compliance setting to JRE 6.

What exactly does compiler compliance level mean?

Jeffrey Bosboom
  • 13,313
  • 16
  • 79
  • 92
Aviv Cohn
  • 15,543
  • 25
  • 68
  • 131

4 Answers4

54

The compiler compliance setting tells the compiler to pretend it's a different version of Java.

The Java 8 compiler will produce class files in the Java 8 version of the class file format, and accept Java 8 source files. JRE 6 can't load this version, because it was created after JRE 6 was.

If you set the compliance level to "JRE 6", it will instead compile Java 6 source files into Java 6 class files.

It's like saving a Word document as "Word 97-2003 format" - so that Word 97-2003 can read your document. You're saving the class files in Java 6 format so that Java 6 can read them.

user253751
  • 57,427
  • 7
  • 48
  • 90
  • 8
    Thanks for your comment. Please confirm if I understand: The compiler compliance setting tells Eclipse which "version of the compiler to use". Setting the compiler compliance level to 6, will make the compiler of version 6, and produce compiled files in the version 6 format. So if I'm using JDK 7, and I want my program to be able to run on JRE 6, than setting the compiler compliance level to JRE 6 will mean that the compiled .class files will be of the format JRE 6 understands. Is this correct? – Aviv Cohn Mar 22 '14 at 23:00
  • 5
    Okay, and why is it also required to set the Java Build Path to use the appropriate older JRE library? Why can't the Java Build Path only include the most recent JRE library? I mean, if I'm coding a program to be able to run on JRE 6, I also need to include JRE 6, in addition to JRE 7, in the Java Build Path of the project (correct?). If so, why? – Aviv Cohn Mar 23 '14 at 10:25
  • 1
    You need to include JRE 6 *instead of* JRE 7. Otherwise you might accidentally use something that exists in 7 but not in 6. – user253751 Mar 24 '14 at 01:33
  • hi immibis, you said it will compile Java 6 source files into Java 6 class files. What if my source code contains Java 8 specific features and i compiled for Java 6. Will the code compiled and run on Java 6? – Dexter Mar 27 '18 at 07:07
  • @Dexter No. The class file version must be equal or newer to the source version. – user253751 Mar 27 '18 at 22:48
8

It tells you what version of the JDK you are adhering to; specifically, which JRE are you targeting with your build.

It is the MINIMUM JRE needed to run your code

Brendan Lesniak
  • 2,271
  • 4
  • 24
  • 48
4

The Java compiler can compile code that will run on prior versions of the JVM. Even if you have JDK 6 installed, you could be writing code targeted for people that use JDK 5. The Compiler Compliance level tells Eclipse to use appropriate settings when compiling your project to ensure you code will work on the target JVM you specify. By default, if I recall, Eclipse picks Java 5 Compliance. If you want to use code features specific to Java 6 or Java 7, you will have to change the compliance level.

djmorton
  • 616
  • 4
  • 6
3

The simplest way to describe the "Java compiler compliance" setting is that it determines which Java Virtual Machine instructions may be used in the "compiled" Java code ... and which library class versions are to be used. Each release of the JVM (and its libraries) may introduce new instructions (the VM) or adjust the class libraries that are delivered along with the JVM.

You should set the setting to the lowest level of JVM that you expect your product to be run on.

ErstwhileIII
  • 4,829
  • 2
  • 23
  • 37