15

I just encountered a strange error when switching the JDK version of a new Project of mine from 7u45 to 8u20. A harmless LogManager declaration at the beginning of my class is being refused with the following error:

The type java.lang.reflect.AnnotatedElement cannot be resolved. It is indirectly referenced from required .class files

This is the code:

public class Class1 {   
    private static Logger log = LogManager.getLogger(Class1.class); 
    ...

Eclipse proposes me to configure the build path, but I have no Idea what to configure because I don't know the underlying problem of that error.

Using the JDK with version 7, everything works fine.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Corsair
  • 1,044
  • 3
  • 10
  • 25

5 Answers5

29

When using JDK 8 and an IDE (or any other code processing tool/framework) with its own compiler, like Eclipse, you have to update the tool to a version with Java 8 support, even if you are not using the newer Java 8 features.

The reason is that the compiler must be able to load the newer class files of the JRE in order to compile your software which references these classes.

Sometimes you can get away with an older compiler when it ignores the newer version number of the class files. But some types will confuse older class file parsers as they use new features, notably AnnotatedElement, which now has default methods, and Map.Entry, an interface which now has static methods.

It seems that Eclipse does not make a difference between references for which no class file could be found and class files it failed to read when saying “«classname» cannot be resolved”.

The same applies to all tools and frameworks using ECJ as embedded compiler.

Holger
  • 285,553
  • 42
  • 434
  • 765
  • Okay, seems logic. Thx! :) – Corsair Sep 30 '14 at 12:37
  • 3
    Just for completeness, the same applies if the class mentioned in the compiler error is [`Map.Entry`](http://docs.oracle.com/javase/8/docs/api/java/util/Map.Entry.html) (as in a lot of questions) as that `interface` now has `static` methods… – Holger Dec 08 '14 at 20:37
  • 1
    You don't have to update your IDE. You can also install an older JDK/JRE and specify that Eclipse use the older version (Window > Preferences > Installed JREs). E.g. I specified C:\Program Files\Java\jdk1.7.0_67\jre there and the error went away. – Jim Jan 26 '15 at 09:20
  • 5
    @Jim: that is a statement without any worth. This answer begins with **When using JDK 8 …** Of course, when you are not using JDK 8, a statement that begins with “When using JDK 8 …” doesn’t apply. – Holger Jan 26 '15 at 09:26
  • 1
    @Holger This happened to me when Java updated on my dev machine. In that case it was not a deliberate choice to switch to a 1.8 JRE and switching back is a manual step. Your answer is fine but it doesn't mention the alternative to "using JDK 8". My comment is complementary to what you've said: if you *are* using JDK 8 - update the IDE. If you *aren't* (or *weren't*) using JDK 8, you don't have to update the IDE - you can just specify a different JRE. – Jim Jan 26 '15 at 10:47
  • 4
    @Jim: I assume that every developer is capable of using on older JDK/JRE and doesn’t need special hints about this option nor how to do that. Compare with the last sentence of the question; the questioner already did this to verify that the problem is tied to the JDK version. – Holger Jan 26 '15 at 11:18
1

If this happens to you in Tomcat running from within Eclipse (question was closed as duplicate and redirects here), go to Preferences → Server → Runtime Environments → Tomcat version → Edit… and make sure the selected JRE matches the Tomcat version. (Maybe you need to install one.)

Community
  • 1
  • 1
Matthias Ronge
  • 9,403
  • 7
  • 47
  • 63
  • Well done for actually providing practical advice as to how to actually rectify the problem! – Geeb Mar 16 '16 at 10:49
1

Solution 1:

Try changing the source level.

  1. Go to Project > Preferences > Java Compiler.
  2. Enable Project specific settings
  3. Set compiler compliance level to 1.4 or below.
  4. Restart

Solution 2:

  1. Create a new workspace.
  2. Copy project files into the new workspace.
  3. Import project into eclipse and rebuild.
Abhishek
  • 370
  • 1
  • 3
  • 14
0

I change from JRE 8 to JRE 7 and restart Eclipse then it works fine.

Duong
  • 19
  • 2
0

I was using Eclipse Helios with JRE 8 and updating the software solve the problem.

Now I'm using Eclipse Mars and works just fine -> https://eclipse.org/downloads/

Jessica
  • 119
  • 5