java.lang.NoClassDefFoundError:无法初始化类org.apache.commons.logging.LogFactory

时间:2014-06-25 05:42:45

标签: java google-app-engine spring-mvc

我是Google App Engine的新手。我收到了这个错误:

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

我添加了slf4j依赖项并在spring-context依赖项中排除了commons-logging,但仍然遇到此错误。该应用程序在我的本地计算机上运行良好,但在部署到App Engine时会出现此错误。

2 个答案:

答案 0 :(得分:1)

感谢this blog,我能够解决此问题:

  

Commons-logging是许多框架的依赖项,包括Spring。在本地服务器上,一切运行正常。 在云端,Google App Engine基础架构将commons-logging-1.1.1.jar替换为具有不同包结构的JAR。 实际上,这意味着即使您将JAR包含在依赖项中,也会在org.apache.commons.logging.LogFactory上获得有趣的NoClassDefFoundError。解决方案仍然是包含类,但要给JAR另一个名称。

     

由于我使用Maven,因此我使用Spring和MyFaces工件的排除标记从WAR中删除了commons-logging依赖项。然后,我添加了对commons-logging的依赖:commons-logging-api:1.1:jar与运行时范围。这个罐子不会被替换。

所以你应该从Spring中排除commons-logging:

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

然后在commons-logging-1.1上添加对运行时范围的依赖:

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

答案 1 :(得分:0)

您在/ war / WEB-INF / lib /文件夹中缺少必需的jar。将它放在类路径中是不够的。

如果使用Eclipse,则应在“问题”选项卡中看到警告。右键单击它,快速修复,选择“复制...”。或者手动将此jar添加到/ lib文件夹。

相关问题