1

My default JRE in Eclipse is JDK 1.7. I also have JDK 1.6 for projects that don't need Java 7. My default compiler compliance level is naturally JDK 1.7, in accordance with the default JRE.

Now, for projects that use Java 6 (configured in Project Properties -> Java Build Path), Eclipse does not automatically use JDK 1.6 compliance under Project Properties -> Java Compiler. Instead, it uses the workspace default 1.7 which leads to the problem described here: Exception in thread "main" java.lang.UnsupportedClassVersionError: a (Unsupported major.minor version 51.0).

Is there a way to configure Eclipse to use the compiler compliance level that matches the chosen JRE (or vice versa) or do I have to manually configure both for all projects that don't use the default JRE?

Community
  • 1
  • 1
rolve
  • 10,083
  • 4
  • 55
  • 75

1 Answers1

3

Preferences -> Java -> Installed JREs -> Execution Environments

There you map installed JVMs to Java language versions (and yours probably maps the Java 6 environment to the Java 7 JVM). Afterwards set your project build path setting to that Execution Environment (not the JRE or workspace default JRE).

rolve
  • 10,083
  • 4
  • 55
  • 75
Bananeweizen
  • 21,797
  • 8
  • 68
  • 88
  • Thanks for the answer. Unfortunately, it doesn't seem to work. First, the mapping from execution environment to VM was already correct. And second, even after changing the build path to use the execution environment instead of the concrete VM, the compiler compliance level property still didn't change automatically and defaults to 1.7. – rolve Sep 27 '12 at 17:14
  • That's interesting, as I verified that on my Juno installation before posting it. For me, switching the execution environment in the build path settings changes all three (disabled) combo box entries on the Java Compiler preference page. – Bananeweizen Sep 27 '12 at 17:24
  • Is *Enable project specific settings* checked in *Java Compiler*? – rolve Sep 27 '12 at 17:28
  • Yes, of course. Sorry if I forgot to mention to set that option. – Bananeweizen Sep 28 '12 at 06:24
  • Well, that's exactly what I was trying to avoid. I was hoping that Eclipse is smart enough to chose the matching compiler setting without the need to set project specific properties. – rolve Sep 28 '12 at 11:41
  • 1
    It cannot. Maybe there is a misunderstanding here: The build path only says what code is visible to the compiler during compilation, it does not saying anything about the compiler _used_. If that really is your main interest, this project specific compiler option is the important one for you, not the build path. – Bananeweizen Sep 28 '12 at 12:41
  • I am pretty sure it can. If you check *Enable project specific settings* under *Java Compiler*, the first option is to match the compliance level to the execution environment. Fine. But there is no setting to make this behavior the default for all projects (which would be the right thing to do, by default, no?). – rolve Sep 28 '12 at 13:25