Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

org.apache.log4j.Logger cannot be resolved inspite of Maven dependencies

I am using maven (i.e. maven plugin for eclipse) for dependency management and generally it works. E.g. jasypt gets strong textresolved and I can use it without any problem. However when trying to use Log4J I get issues like "The import org.apache.log4j cannot be resolved" even though I followed the exact steps as desribed on http://logging.apache.org/log4j/2.x/maven-artifacts.html.

There it says I need to include the following snippet into my pom.xml

<dependencies>
  <dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-api</artifactId>
    <version>2.6.2</version>
  </dependency>
  <dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-core</artifactId>
    <version>2.6.2</version>
  </dependency>
</dependencies>

However exactly that is included in my pom.xml as given below in my full pom.xml. Does that make sense to anyone? I also tried replacing the two dependencies with the following however without any success

<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j</artifactId>
    <version>2.6.2</version>
    <type>pom</type>
</dependency>

Cheers Tom

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>PCenter</groupId>
  <artifactId>PCenter</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>
  <name>PCenter</name>
  <build>
    <sourceDirectory>src</sourceDirectory>
    <resources>
      <resource>
        <directory>src</directory>
        <excludes>
          <exclude>**/*.java</exclude>
        </excludes>
      </resource>
      <resource>
        <directory>ejbModule</directory>
        <excludes>
          <exclude>**/*.java</exclude>
        </excludes>
      </resource>
    </resources>
    <plugins>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.5.1</version>
        <configuration>
          <source>1.8</source>
          <target>1.8</target>
        </configuration>
      </plugin>
      <plugin>
        <artifactId>maven-war-plugin</artifactId>
        <version>2.6</version>
        <configuration>
          <warSourceDirectory>WebContent</warSourceDirectory>
          <failOnMissingWebXml>false</failOnMissingWebXml>
        </configuration>
      </plugin>
    </plugins>
  </build>
  <dependencies>
    <dependency>
        <groupId>com.paypal.sdk</groupId>
        <artifactId>rest-api-sdk</artifactId>
        <version>1.9.2</version>
    </dependency>
    <dependency>
        <groupId>org.jasypt</groupId>
        <artifactId>jasypt</artifactId>
        <version>1.9.2</version>
    </dependency>
    <dependency>
        <groupId>org.apache.axis</groupId>
        <artifactId>axis</artifactId>
        <version>1.4</version>
    </dependency>
    <dependency>
        <groupId>org.apache.axis</groupId>
        <artifactId>axis-jaxrpc</artifactId>
        <version>1.4</version>
    </dependency>
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-api</artifactId>
        <version>2.6.2</version>
    </dependency>
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-core</artifactId>
        <version>2.6.2</version>
    </dependency>
  </dependencies>
</project>
like image 366
TomFree Avatar asked Dec 10 '22 15:12

TomFree


2 Answers

The problem is that you have Log4j2 in the dependencies, and use the older, Log4j 1.2.x API.

  • Either depend on Log4j 1.2.x

    <dependencies>
      <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>1.2.17</version>
      </dependency>
    </dependencies>
    
  • Or use the correct API calls for Log4j 2.x:

Calls to org.apache.log4j.Logger.getLogger() must be modified to org.apache.logging.log4j.LogManager.getLogger()

(From Migrating from Log4j 1.x)

like image 126
ppeterka Avatar answered Dec 13 '22 05:12

ppeterka


Hmm, actually I tried ppeterkas suggested solution with including that older 1.2x api, but that didn't work for me, I still had that problem. And since the code which could not be compiled was not really my code but rather some open source code that I happened to be using in my own code, I didn't really want to modify it's loggers (I should also mention, that I just started having this problem after newly opening my eclipse on a new computer, before it was actually compiling fine with the same pom.xml, so that's weird, but nevertheless it still needed fixing then...).

So a solution for me was to include an Apache Log4j 1.x Compatibility API for log4j 2.7:

http://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-1.2-api/2.7

<!-- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-1.2-api -->
<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-1.2-api</artifactId>
    <version>2.7</version>
</dependency>

Hope this will help other people having the same problem.

like image 31
Arturas M Avatar answered Dec 13 '22 05:12

Arturas M