在 Java 8 中使用 LogManager (l4j2) 时出错(无法解析 java.lang.reflect.AnnotatedElement)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26102115/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 09:17:22  来源:igfitidea点击:

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

javajava-7java-8log4j2

提问by Corsair

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:

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

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

无法解析 java.lang.reflect.AnnotatedElement 类型。它是从所需的 .class 文件间接引用的

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.

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

Using the JDK with version 7, everything works fine.

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

回答by Holger

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.

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

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.

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

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 defaultmethods, and Map.Entry, an interfacewhich now has staticmethods.

有时,当旧编译器忽略类文件的较新版本号时,您可以摆脱它。但是某些类型会混淆旧的类文件解析器,因为它们使用新功能,特别是AnnotatedElement,现在有default方法,而Map.Entryinterface现在有static方法。

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

似乎 Eclipse 在找不到类文件的引用和它在说“ ?classname?无法解决”。

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

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

回答by Abhishek Muralidharan

Solution 1:

解决方案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
  1. 转到项目 > 首选项 > Java 编译器。
  2. 启用项目特定设置
  3. 将编译器合规性级别设置为 1.4 或更低。
  4. 重新开始

Solution 2:

解决方案2:

  1. Create a new workspace.
  2. Copy project files into the new workspace.
  3. Import project into eclipse and rebuild.
  1. 创建一个新的工作区。
  2. 将项目文件复制到新工作区。
  3. 将项目导入eclipse并重建。

回答by Matthias Ronge

If this happens to you in Tomcat running from within Eclipse (questionwas 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.)

如果在 Eclipse 中运行的 Tomcat 中发生这种情况(问题已关闭为重复并在此处重定向),请转到首选项 → 服务器 → 运行时环境 → Tomcat 版本→ 编辑...并确保所选的 JRE 与 Tomcat 版本匹配。(也许你需要安装一个。)

回答by Duong

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

我从 JRE 8 更改为 JRE 7 并重新启动 Eclipse,然后它工作正常。

回答by Jessica

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

我使用带有 JRE 8 的 Eclipse Helios 并更新软件解决了问题。

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

现在我正在使用 Eclipse Mars 并且工作正常 -> https://eclipse.org/downloads/