Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

unexpected exception: java.lang.NoClassDefFoundError: org/apache/log4j/LogManager

I'm developing a GWT application. It's using RPC to collect information from an internal system. It does so by using a library jar, lets call it alpha.jar. We are using this jar in many application so it works fine, and btw its built with ANT, outside eclipse.

Some classes in alpha.jar references LOG4J2 and also lots of other external jars, so when we run an application we pass a classpath to all those, and everything works out fine. Please note that this is not a simple beginners problem. The alpha.jar is working as it should, including calls to Log4J.

The problem:

In Eclipse, I have this GWT application project and also the Alpha.jar project (with source code of course). The server part needs to instatiate alpha objects and communicate to the alpha system.

When do this in GWT by adding a build-path-reference to the Alpha project, my GWT app runs fine.

When I instead of the project reference include (in war/WEB-INF/lib) the alpha.jar and runs the app, I get the error in the title the first time I instantiate a class from alpha.jar.

There are no peculiarities in how the alpha.jar is built, so basically it should be the same thing as the project in eclipse, right?

Note the following:

*) The alpha.jar's dependent jars are also in war/WEB-INF/lib. log4j2-core, log4j-api as well as a bunch of others (apache common for example)

*) If I remove the alpha.jar (and the code that calls it) and instead just add code that called LOG4J2, that code also works fine!

How come I get this weird error when using the JAR?? Note also the NoClassDefFoundError, its not the more common ClassNotFoundException. Pls see What causes and what are the differences between NoClassDefFoundError and ClassNotFoundException?

If you need more info let me know.

like image 663
Peter Andersson Avatar asked Nov 29 '25 17:11

Peter Andersson


2 Answers

org.apache.log4j.LogManager is a class from log4j 1.2 (not log4j2).

Therefore, one of your web app jars must be referencing it. The culprit should be visible in the stack trace.

Depending upon your circumstances, you may want to just add a log4j 1.2 jar to the web app as the two versions are completely independent of each other.

like image 200
Steve C Avatar answered Dec 01 '25 07:12

Steve C


as refered before:

org.apache.log4j.LogManager is a class from log4j 1.2 (not log4j2).

this problem meets when you combine use log4j 1.2 and log4j 2.x, maybe. so you have to add bridge api to you project.

this is a Migrating problem.

  • Log4j – Migrating from Log4j 1.x - Apache Log4j 2

add those to you pom.xml

        <log4j2.version>2.7</log4j2.version>
        <disruptor.version>3.3.6</disruptor.version>

        <!--log4j2 dependencies -->
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-api</artifactId>
            <version>${log4j2.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
            <version>${log4j2.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-1.2-api</artifactId>
            <version>${log4j2.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-slf4j-impl</artifactId>
            <version>${log4j2.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-jcl</artifactId>
            <version>${log4j2.version}</version>
        </dependency>
        <dependency>
            <groupId>com.lmax</groupId>
            <artifactId>disruptor</artifactId>
            <version>${disruptor.version}</version>
        </dependency>

then, you could use mvn dependency:resolve to see no log4j 1.2

[INFO] The following files have been resolved:
[INFO]    org.springframework.data:spring-data-redis:jar:1.7.2.RELEASE:compile
[INFO]    org.apache.logging.log4j:log4j-api:jar:2.7:compile
[INFO]    org.apache.logging.log4j:log4j-slf4j-impl:jar:2.7:compile
[INFO]    com.lmax:disruptor:jar:3.3.6:compile
[INFO]    org.apache.logging.log4j:log4j-1.2-api:jar:2.7:compile
[INFO]    javax.mail:mail:jar:1.4.5:compile
[INFO]    org.springframework:spring-tx:jar:4.3.1.RELEASE:compile
[INFO]    org.apache.logging.log4j:log4j-core:jar:2.7:compile
[INFO]    org.apache.logging.log4j:log4j-jcl:jar:2.7:compile
[INFO]    javax.activation:activation:jar:1.1:compile
[INFO]    org.springframework:spring-beans:jar:4.3.1.RELEASE:compile
[INFO]    org.springframework:spring-web:jar:4.3.1.RELEASE:compile
[INFO]    org.springframework:spring-webmvc:jar:4.3.1.RELEASE:compile
[INFO]    org.springframework:spring-oxm:jar:4.2.6.RELEASE:compile
[INFO]    org.springframework:spring-jdbc:jar:4.3.1.RELEASE:compile
[INFO]    com.alibaba:fastjson:jar:1.2.4:compile
[INFO]    mysql:mysql-connector-java:jar:5.1.21:compile
[INFO]    org.apache.tomcat:tomcat-servlet-api:jar:7.0.54:provided
[INFO]    org.slf4j:slf4j-api:jar:1.7.21:compile
[INFO]    org.springframework:spring-context-support:jar:4.3.1.RELEASE:compile
[INFO]    commons-beanutils:commons-beanutils:jar:1.8.3:compile
[INFO]    org.springframework:spring-context:jar:4.3.1.RELEASE:compile
[INFO]    org.hamcrest:hamcrest-core:jar:1.3:test
[INFO]    redis.clients:jedis:jar:2.8.1:compile
[INFO]    org.springframework:spring-expression:jar:4.3.1.RELEASE:compile
[INFO]    org.springframework.data:spring-data-commons:jar:1.12.2.RELEASE:compile
[INFO]    org.springframework.data:spring-data-keyvalue:jar:1.1.2.RELEASE:compile
[INFO]    junit:junit:jar:4.12:test
[INFO]    org.springframework:spring-core:jar:4.3.1.RELEASE:compile
[INFO]    commons-logging:commons-logging:jar:1.2:compile
[INFO]    org.springframework:spring-aop:jar:4.3.1.RELEASE:compile
[INFO]    org.apache.commons:commons-pool2:jar:2.4.2:compile
[INFO]    org.slf4j:jcl-over-slf4j:jar:1.7.21:runtime

refers:

  • Log4j 1.x Adaptor – Log4j 1.2 Bridge - Apache Log4j 1.x Compatibility API

  • Log4j Commons Logging Adaptor – Commons Logging Bridge - Apache Log4j Commons Logging Bridge

  • SLF4J Binding Using Log4j – Log4j 2 SLF4J Binding - Apache Log4j SLF4J Binding
like image 38
Mark Simon Avatar answered Dec 01 '25 07:12

Mark Simon