質問

I've got a problem with NoClassDefFound exception in Grails 2.0 when I tried to use library from external JAR.

I've checked that declared JARs are inside of created WAR, also grials dependecies-report do not marks any issues with that.

Locally added JARs or downloaded from Maven repo seems no difference. I've also tried to clean IVY cache and clean grails project without success.

Did you got any ideas how to fix it?


BuildConfig.groovy (part of)

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")
    }
}

Exception

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

Message Could not initialize class com.google.gdata.client.youtube.YouTubeService

役に立ちましたか?

解決

NoClassDefFoundError is not the same as ClassNotFoundException. Getting a ClassNotFoundException means the class isn't there, so you have a straightforward jar/dependency problem. NoClassDefFoundError means that the specified class was found, but that a class that it references wasn't found. It's a much more frustrating issue to track down because the JVM doesn't tell you what's missing.

You need to make sure that you have all of the dependencies of the class that's failing to load, and all of their dependencies, etc.

他のヒント

You have all your dependencies declared both in compile and runtime scope. Each dependency should be declared only once. If you declare a dependency in compile scope, it will also be available runtime. Since you need this class for compilation, you should keep com.google.gdata:gdata-youtube:2.0 under 'compile', and remove it from 'runtime'

A description of the available scopes, taken from the user documentation:

  • build: Dependencies for the build system only
  • compile: Dependencies for the compile step
  • runtime: Dependencies needed at runtime but not for compilation (see above)
  • test: Dependencies needed for testing but not at runtime (see above)
  • provided: Dependencies needed at development time, but not during WAR deployment
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top