本文介绍了将LogManager(l4j2)与Java 8一起使用时出错(无法解析java.lang.reflect.AnnotatedElement)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将新项目的JDK版本从7u45切换到8u20时遇到了一个奇怪的错误。我的类开头的无害LogManager声明被拒绝,并出现以下错误:

这是代码:

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

Eclipse建议我配置构建路径,但我不知道配置什么因为我不知道该错误的根本问题。



将JDK与版本7一起使用,一切正常。

解决方案

当使用JDK 8和IDE(或任何其他代码处理工具/框架)和自己的编译器(如Eclipse)时即使您没有使用较新的Java 8功能,也必须将该工具更新为支持Java 8的版本。



原因是编译器必须能够加载JRE的新类文件,以便编译引用这些类的软件。



有时你可以在忽略较新的版本时使用较旧的编译器类文件的数量。但是有些类型会因为使用新功能而混淆较旧的类文件解析器,特别是,现在有默认方法,,界面现在有静态方法。



似乎Eclipse没有找到可以找到类文件的引用和它在说«classname»无法解析时无法读取的类文件之间的区别。 / p>

这同样适用于使用ECJ作为嵌入式编译器的所有工具和框架。


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:

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.

解决方案

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.

这篇关于将LogManager(l4j2)与Java 8一起使用时出错(无法解析java.lang.reflect.AnnotatedElement)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-29 19:33