Error when using LogManager (l4j2) with Java 8 (java.lang.reflect.AnnotatedElement cannot be resolved)

纵饮孤独 提交于 2019-12-13 04:55:42

问题


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.


回答1:


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.




回答2:


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.)




回答3:


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




回答4:


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/




回答5:


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.


来源:https://stackoverflow.com/questions/26341433/error-while-starting-tomcat-application-the-type-java-util-mapentry-cannot-be

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!