简体   繁体   中英

java.lang.NoClassDefFoundError: Could not initialize class org.apache.commons.logging.LogFactory

I am new to Google App Engine. I am getting this error :

java.lang.NoClassDefFoundError: Could not initialize class 
                  org.apache.commons.logging.LogFactory at
org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:282) at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:106) at 
org.mortbay.jetty.handler.ContextHandler.startContext(ContextHandler.java:548) at 
org.mortbay.jetty.servlet.Context.startContext(Context.java:136) at ...

I have added slf4j dependencies and excluded commons-logging in spring-context dependency but still getting this error. The app works perfectly fine on my local machine but gives me this error when deployed to the App Engine.

Thanks to this blog , I was able to resolve this issue :

Commons-logging is a dependency of many frameworks, Spring included. On the local server, everything runs fine. In effect, that means you get funny NoClassDefFoundError on org.apache.commons.logging.LogFactory even though you included the JAR as a dependency. The solution is to still include the classes, but to give the JAR another name.

Since I use Maven, I removed the commons-logging dependency from the WAR with the exclusion tag for Spring and MyFaces artifact. Then, I added a dependency on commons-logging:commons-logging-api:1.1:jar with the runtime scope. This jar won't be replaced.

So you should exclude commons-logging from Spring :

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>
    <version>${org.springframework.version}</version>
    <exclusions>
        <exclusion>
            <groupId>commons-logging</groupId>
            <artifactId>commons-logging</artifactId>
        </exclusion>
    </exclusions>
</dependency>

Then add a dependency on commons-logging-1.1 with runtime scope:

<dependency>
        <groupId>commons-logging</groupId>
        <artifactId>commons-logging-api</artifactId>
        <version>1.1</version>
        <scope>runtime</scope>
</dependency>

You are missing a required jar in your /war/WEB-INF/lib/ folder. It's not enough to have it in your classpath.

If you use Eclipse, you should see a warning in the Problems tab. Right click on it, Quick Fix, select " Copy ...". Or add this jar to the /lib folder manually.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM