导入org.apache.log4j无法解析 [英] The import org.apache.log4j cannot be resolved

查看:2779
本文介绍了导入org.apache.log4j无法解析的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Eclipse,Selenium,W​​indows

导入org.apache.log4j无法解析

我已按照我可以找到的所有记录的步骤进行操作,即:

1)以下两个jar文件位于项目的构建路径中: log4j-core-2.6.2.jar和log4j-api-2.6.2.jar

2)上面的两个jar文件位于以下文件夹中: C:\ eclipse \ apache-log4j-2.6.2-bin

3)上面的文件夹位于我的系统CLASSPATH环境变量C:\ eclipse \ apache-log4j-2.6.2-bin

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)我已经清理了项目

6)导入失败是:import org.apache.log4j.Logger;


间歇地我也收到此错误消息

第一行无法解析org.apache.log4j.Logger类型.它是从所需的.class文件中间接引用的".

package com.example;

当我做一个项目时,此错误消失->清理

但是导入log4j的错误仍然存​​在.


任何想法都将不胜感激.

解决方案

如果收到此错误,则意味着您的应用程序或其依赖项之一使用Log4j 1.2 API.

org.apache.log4j软件包是Log4j 1.2命名空间. Log4j 2使用org.apache.logging.log4j命名空间. Log4j 1.2于2015年停产,建议用户升级到Log4j 2. /p>

您的类路径中已经有正确的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实现.

请确保从类路径中删除了旧的log4j-1.2.x.jar.

最后,显示的配置似乎是针对Log4j 1.2的,不适用于Log4j2.Log4j2手册具有许多示例配置. (仅文档是升级的原因,它在Log4j 2中要好得多.)例如,请参见文件附加器.

Log4j 2还支持属性配置格式 (但是语法与log4j 1.2属性格式不同).

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

这篇关于导入org.apache.log4j无法解析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆