问题描述
导入org.apache.log4j无法解决
我遵循了我能找到的所有记录的步骤,即:
1)以下两个JAR文件在项目构建路径中: log4j-core-2.6.2.jar和log4j-api 2.6.2.2.jar
2)上面的两个JAR文件位于以下文件夹中: C:\ eclipse \ apache-log4j-2.6.2 bin
3)上面的文件夹在我的System classPath环境中变量C:\ eclipse \ apache-log4j-2.6.6.2 bin
4)该项目包含以下log4j.properties文件root
# 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)我已经清洁了该项目
6)导入失败是:导入org.apache.log4j.logger;
间歇性我也收到此错误消息
" type org.apache.log4j.logger无法解决.它是从必需的.class文件中间接引用的:
软件包com.example;
当我进行项目 - > Clean
时,此错误消失了但是log4j导入的错误持续存在.
任何想法都将不胜感激.
推荐答案
如果您遇到此错误,则意味着您的应用程序或其依赖项之一使用Log4J 1.2 API.
org.apache.log4j软件包是log4j 1.2名称空间. log4j 2使用org.apache.logging.log4j命名空间. log4j 1.2在2015年成为寿命 2015年,建议用户升级到log4j 2.
您已经拥有正确的log4j-api2.x.jar和log4j-core-2.x.jar在您的类路径中,唯一缺少的是log4j-1.2-api-2.x.jar. Log4J-1.2-API是一个适配器模块,可确保您的应用程序对Log4J 1.2 API的调用委派给Log4J 2实现.
请确保从类路径中删除旧的log4j-1.2.x.jar.
最后,您显示的配置似乎适用于log4j 1.2,并且与log4j 2不起作用2. log4j 2手册具有许多示例配置. (仅文档就是升级的原因,它在log4j 2中要好得多. . " noreferrer">文件appender .
log4j 2还支持 properties配置格式 (但是语法与log4j 1.2属性格式不同).
其他推荐答案
检查您的Eclipse版本和Log4J依赖项版本.如果1.2.17不起作用,请使用1.2.16.
检查问题描述
Eclipse, Selenium, Windows
The import org.apache.log4j cannot be resolved
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
2) the above two jar files are located in the following folder: 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
4) The project contains the following log4j.properties file in the project root
# 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
6) failing import is: 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:
package com.example;
This error disappears when I do a project -> Clean
but the error on import of log4j persists.
Any thoughts would be greatly appreciated.
推荐答案
If you are getting this error it means your application or one of its dependencies uses the 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 Life in 2015 and users are recommended to upgrade to 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.
Please ensure the old log4j-1.2.x.jar is removed from the classpath.
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 appender or the File appender.
Log4j 2 also supports a properties configuration format (but the syntax differs from the log4j 1.2 properties format).
其他推荐答案
Check your Eclipse version and log4j dependency version. If 1.2.17 is not working then check using 1.2.16.