Java 导入 org.apache.log4j 无法解析
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38360874/
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
The import org.apache.log4j cannot be resolved
提问by Charles F Radley
Eclipse, Selenium, Windows
日食、硒、视窗
The import org.apache.log4j cannot be resolved
导入 org.apache.log4j 无法解析
I have followed all the documented steps I can find, i.e.:
我遵循了我能找到的所有记录步骤,即:
1) the following two jar files are in the project build-path: log4j-core-2.6.2.jar and log4j-api-2.6.2.jar
1)项目build-path下有如下两个jar文件:log4j-core-2.6.2.jar和log4j-api-2.6.2.jar
2) the above two jar files are located in the following folder: C:\eclipse\apache-log4j-2.6.2-bin
2)以上两个jar文件位于以下文件夹:C:\eclipse\apache-log4j-2.6.2-bin
3) The above folder is in my system CLASSPATH environment variable C:\eclipse\apache-log4j-2.6.2-bin
3)以上文件夹在我的系统CLASSPATH环境变量C:\eclipse\apache-log4j-2.6.2-bin
4) The project contains the following log4j.properties file in the project root
4)项目在项目根目录下包含如下log4j.properties文件
# This sets the global logging level and specifies the appenders
log4j.rootLogger=INFO, theConsoleAppender
# settings for the console appender
log4j.appender.theConsoleAppender=org.apache.log4j.ConsoleAppender
log4j.appender.theConsoleAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.theConsoleAppender.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n
5) I have cleaned the project
5)我已经清理了项目
6) failing import is: import org.apache.log4j.Logger;
6)导入失败是:import org.apache.log4j.Logger;
Intermittently I am also getting this error message
间歇性地我也收到此错误消息
"The type org.apache.log4j.Logger cannot be resolved. It is indirectly referenced from required .class files" at the first line:
“无法解析 org.apache.log4j.Logger 类型。它是从所需的 .class 文件间接引用的”在第一行:
package com.example;
包com.example;
This error disappears when I do a project -> Clean
当我做一个项目时,这个错误消失了 -> Clean
but the error on import of log4j persists.
但是导入 log4j 时的错误仍然存在。
Any thoughts would be greatly appreciated.
任何想法将不胜感激。
回答by Remko Popma
If you are getting this error it means your application or one of its dependencies uses the Log4j 1.2 API.
如果您收到此错误,则意味着您的应用程序或其依赖项之一使用 Log4j 1.2 API。
The org.apache.log4j
package is the Log4j 1.2 namespace. Log4j 2 uses the org.apache.logging.log4j
namespace. Log4j 1.2 became End of Lifein 2015 and users are recommended to upgrade to Log4j 2.
该org.apache.log4j
包是 Log4j 1.2 命名空间。Log4j 2 使用org.apache.logging.log4j
命名空间。 Log4j 1.2于 2015 年终止,建议用户升级到 Log4j 2。
You already have the correct log4j-api-2.x.jar and log4j-core-2.x.jar in your classpath, the only thing missing is the log4j-1.2-api-2.x.jar. log4j-1.2-api is an adapter module that ensures your application's calls to the Log4j 1.2 API are delegated to the Log4j 2 implementation.
您的类路径中已经有正确的 log4j-api-2.x.jar 和 log4j-core-2.x.jar,唯一缺少的是 log4j-1.2-api-2.x.jar。log4j-1.2-api 是一个适配器模块,可确保您的应用程序对 Log4j 1.2 API 的调用委托给 Log4j 2 实现。
Please ensure the old log4j-1.2.x.jar is removed from the classpath.
请确保从类路径中删除旧的 log4j-1.2.x.jar。
Finally, the configuration you showed seems to be for Log4j 1.2 and will not work with Log4j 2. The Log4j 2 manual has many example configurations. (The documentation alone is reason to upgrade, it is so much better in Log4j 2.) See for example the section on the Console appenderor the File appender.
最后,您展示的配置似乎适用于 Log4j 1.2,不适用于 Log4j 2。Log4j 2 手册有许多示例配置。(文档本身就是升级的原因,它在 Log4j 2 中要好得多。)例如参见Console appender或File appender 部分。
Log4j 2 also supports a properties configuration format(but the syntax differs from the log4j 1.2 properties format).
Log4j 2 还支持属性配置格式(但语法与 log4j 1.2 属性格式不同)。