Search code examples
grailsgroovydependenciesnoclassdeffounderrorbuildconfig

Grails 2.0 depedencies NoClassDefFound issue


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


Solution

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