What is "compiler compliance level" in Eclipse?

JavaEclipse

Java Problem Overview


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?

Java Solutions


Solution 1 - Java

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.

Solution 2 - Java

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

Solution 3 - Java

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.

Solution 4 - Java

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAviv CohnView Question on Stackoverflow
Solution 1 - Javauser253751View Answer on Stackoverflow
Solution 2 - JavaBrendan LesniakView Answer on Stackoverflow
Solution 3 - JavadjmortonView Answer on Stackoverflow
Solution 4 - JavaErstwhileIIIView Answer on Stackoverflow