17

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.

Community
  • 1
  • 1
Peter Andersson
  • 1,947
  • 3
  • 28
  • 44
  • Can you provide your build script ... you are probably missing a Log4J dependency needed to log messages. And check your Java runtime/SDK in Eclipse and Ant (should be the same) – Drejc Oct 13 '14 at 11:02
  • No I'm not missing a dependency. Please read question again. – Peter Andersson Oct 13 '14 at 11:22
  • 1
    Sorry but from the description of your problem I'm not able to understand what exactly are you doing and where the problem is - thus nobody answered your question so far. – Drejc Oct 13 '14 at 13:31
  • This other answer might be more relevant to some people with this issue. https://stackoverflow.com/questions/32368758/log4j2-java-lang-noclassdeffounderror-org-apache-logging-log4j-logmanager?noredirect=1&lq=1 – snowstreams Jan 05 '18 at 17:23

3 Answers3

17

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.

Steve C
  • 18,876
  • 5
  • 34
  • 37
  • Sounds very promising, I missed that it said log4j and not log4j2! I can add 1.2 (but would like to avoid it). How can I find out which JAR is referencing 1.2?? In the other non-GWT apps, our "lib" directory contains only log4j2-core.jar and log4j-api.jar and I dont get this exception there? And why don't I see this problem when using the Project-reference in Eclipse, I am certainly not using 1.2 anywhere. – Peter Andersson Oct 13 '14 at 14:10
  • 4
    Can someone clarify? log4j.Manager may be a log4j construct, but it is found in the log4j v2.x jar files. The log4j 2.x are log4j2 constructs, correct? – Richard Jessop Nov 08 '19 at 21:29
2

If you have use log4j 2 please do not forget to name your log4j2.xml instead of log4j.xml

albgorski
  • 107
  • 3
1

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.

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:

Mark Simon
  • 362
  • 4
  • 11