The import org.apache.log4j cannot be resolved

55,844

Solution 1

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

Solution 2

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

Share:
55,844
Charles F Radley
Author by

Charles F Radley

Professional Summary • Seasoned post-graduate professional, technical QA practitioner • Proficient in QA, audits, test planning development, methods, tools and scripts for enterprise and client software and firmware • 15 years’ experience in Software QA, test and validation using a diverse range of tools and methodologies, including Agile, ISO 9000-3 and SEI-CMM Level-3 • Specialized in developing and implementing thorough test strategies, using a combination of manual testing and automation • Proficient in tools and processes to identify important defects, and mentoring others in them Education • M.Sc., Systems Engineering, The City University, London, UK • B.Sc., Physics, Imperial College, University of London, London, UK Professional / Technical / Certifications • 2014 ISTQB – ASTQB Certified Tester – Foundation Level • 2003 Rational TeamTest Certification • 1997 Intersolv/Merant/Serena Version Manager Skillset • Rational Tools: Clearcase, Clearquest, Requisite Pro, Team Concert, Robot, Purecoverage, Test Studio • Test Tools: Selenium, Rational Test Studio, Perl, Silk Test (4Test), Silk Performer; Jtest, Test Director, Mercury Quality Center, Selenium, VMWare, JIRA, Rally, Csh, Ksh, Bash, Java • QA/CM Tools: Visual SourceSafe, PVCS-VM, RCS/CVS, IBM-CMVC, Team Foundation Server (Ms TFS) • Operating Systems: Windows-9X/NT/2K/XP/.net/Vista; DOS; Solaris, HPUX, Linux (several distros)

Updated on July 09, 2022

Comments

  • Charles F Radley
    Charles F Radley almost 2 years

    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.

  • 2c00L
    2c00L over 2 years
    This was helpful. Problem with eclipse is that it doesn't exactly say which dependency is infact using that, it just says something is referencing the missing library and gives up build with an generic error. Leaving it up to the developer to manually debug.