Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The import org.apache.log4j cannot be resolved

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.

like image 451
Charles F Radley Avatar asked Jul 13 '16 20:07

Charles F Radley


People also ask

Does log4j 1.2 17 have vulnerability?

Included in Log4j 1.2 is a SocketServer class that is vulnerable to deserialization of untrusted data which can be exploited to remotely execute arbitrary code when combined with a deserialization gadget when listening to untrusted network traffic for log data.


2 Answers

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

like image 72
Remko Popma Avatar answered Oct 08 '22 07:10

Remko Popma


Check your Eclipse version and log4j dependency version. If 1.2.17 is not working then check using 1.2.16.

like image 1
user14997828 Avatar answered Oct 08 '22 06:10

user14997828