【问题标题】:Grails 2.0 depedencies NoClassDefFound issueGrails 2.0 依赖 NoClassDefFounderror 问题
【发布时间】:2012-04-07 08:43:15
【问题描述】:

当我尝试使用来自外部 JAR 的库时,我遇到了 Grails 2.0 中 NoClassDefFound 异常的问题。

我已经检查了声明的 JAR 是否在创建的 WAR 中,grials 的依赖报告也没有标记任何问题。

本地添加的 JAR 或从 Maven 存储库下载似乎没有区别。我也尝试过清理 IVY 缓存和清理 grails 项目,但没有成功。

您有任何解决方法的想法吗?


BuildConfig.groovy(部分)

grails.project.dependency.resolution = {

    inherits("global") {
        // uncomment to disable ehcache
        // excludes 'ehcache'
    }
    log "error" // log level of Ivy resolver, either 'error', 'warn', 'info', 'debug' or 'verbose'
    checksums true // Whether to verify checksums on resolve

    repositories {
        inherits true // Whether to inherit repository definitions from plugins
        grailsPlugins()
        grailsHome()
        grailsCentral()

        mavenCentral()
        mavenLocal()
        mavenRepo "http://snapshots.repository.codehaus.org"
        mavenRepo "http://repository.codehaus.org"
        mavenRepo "http://download.java.net/maven/2/"
        mavenRepo "http://repository.jboss.com/maven2/"
    }

    dependencies {

        compile (   "javax:activation:1.0",
                    "javax:mail:1.0",
                    "com.google.gdata:gdata-core:1.0",
                    "com.google.gdata:gdata-client:1.0",
                    "com.google.gdata:gdata-media:1.0",
                    "com.google.gdata:gdata-youtube:2.0"
        )

        runtime (   "javax:activation:1.0",
                    "javax:mail:1.0",
                    "com.google.gdata:gdata-core:1.0",
                    "com.google.gdata:gdata-client:1.0",
                    "com.google.gdata:gdata-media:1.0",
                    "com.google.gdata:gdata-youtube:2.0"
        )
    }

...

}

LibraryController.groovy

import com.google.gdata.client.youtube.YouTubeService
import com.google.gdata.data.youtube.VideoEntry
import com.google.gdata.util.ServiceException

class LibraryController {

    private YouTubeService service
    private static final API_URL = "http://gdata.youtube.com/feeds/api/videos/"

    def index = {
        service = new YouTubeService("app")
    }
}

例外

Class
    java.lang.NoClassDefFoundError
Message
    Could not initialize class com.google.gdata.client.youtube.YouTubeServiceClass
java.lang.NoClassDefFoundError

消息 无法初始化 com.google.gdata.client.youtube.YouTubeService 类

【问题讨论】:

  • 我通常只使用 'jar -tf' 来检查 war 文件中的 jar 文件,以确保包含类文件 YouTubeServiceClass。
  • 什么时候出现这个错误?在运行时使用run-app?
  • 是的,我正在使用运行应用程序,进入库视图时出现异常。
  • 我在任何存储库中都找不到 javax:mail:1.0。确定不是 javax.mail:mail?
  • 我也没有在任何回购中找到谷歌罐子。它们是否手动安装到您的本地 Maven 缓存中?是否使用了 4 个自定义 maven 存储库?

标签: grails groovy dependencies noclassdeffounderror buildconfig


【解决方案1】:

NoClassDefFoundErrorClassNotFoundException 不同。获得ClassNotFoundException 意味着该类不存在,因此您有一个简单的 jar/依赖性问题。 NoClassDefFoundError 表示找到了指定的类,但没有找到它引用的类。这是一个更令人沮丧的问题,因为 JVM 不会告诉您缺少什么。

您需要确保您拥有加载失败的类的所有依赖项,以及它们的所有依赖项等。

【讨论】:

【解决方案2】:

您已在编译和运行时范围内声明了所有依赖项。每个依赖项只能声明一次。如果您在编译范围内声明依赖项,它也将在运行时可用。由于您需要此类进行编译,因此您应该将 com.google.gdata:gdata-youtube:2.0 保留在“编译”下,并将其从“运行时”中删除

可用范围的描述,取自user documentation

  • 构建:仅构建系统的依赖项
  • compile:编译步骤的依赖项
  • runtime:运行时需要依赖但编译不需要(见上文)
  • test:测试所需的依赖项,但在运行时不需要(见上文)
  • 提供:开发时需要依赖项,但在 WAR 部署期间不需要

【讨论】:

  • 我只留下了“编译”范围,但仍然出现异常。
猜你喜欢
  • 1970-01-01
  • 2011-03-08
  • 2012-03-06
  • 2016-10-16
  • 1970-01-01
  • 2019-05-04
  • 2012-03-16
  • 2012-05-21
相关资源
最近更新 更多