Jackson dependency: NoClassDefFoundError

I have added the following to my gradle dependencies:

compile "com.fasterxml.jackson.core:jackson-core:2.7.0"; compile "com.fasterxml.jackson.core:jackson-annotations:2.7.0"; compile "com.fasterxml.jackson.core:jackson-databind:2.7.0";

But when I use Jackson classes, NoClassDefFoundError is being thrown, when the plugin is run.

Is Jackson already included in Sponge? If not you’ll need to include it in your jar file.

Is there a way to do this with gradle (so that it’s always up to date)?

You can include a dependency within your JarFile like so:

Add

configurations {
    shadow
    compile.extendsFrom shadow
}

To your build.gradle (somewhere before the dependencies block)

Add

jar {
    configurations.shadow.each { slice ->
        from(project.zipTree(slice)) {
        }
    }
}

To your build.gradle, or if the ‘jar’ block already exists, just add the configuration in the one above.

You should then be able to declare a dependency like so: “shadow ‘some-dependency’” and it should be included in your jar…

Please note, depending on the license of a project, redistribution is illegal, however Jackson is under the Apache 2 license, so your good.

How do I fix “Could not find property ‘shadow’ on configuration container.”?

Did you name it configuration instead of configurations? (just a guess)
otherwise, show your build.gradle

    group 'MCore'
    version '1.0-SNAPSHOT'

    apply plugin: 'java'

    version = '1.0'

    repositories {
        mavenCentral()
        maven { url 'http://repo.spongepowered.org/maven' }
        maven { url 'http://repo1.maven.org/maven2'}
    }


    sourceSets {
        main {
            java {
                srcDirs = ['MCore/src/main/java']
            }
        }

        test {
            java {
                srcDirs = ['MCore/src/main/java']
            }
        }
    }

    jar {
        configurations.shadow.each { slice ->
            from(project.zipTree(slice)) {
            }
        }
    }

    configurations {
        shadow
        compile.extendsFrom shadow
    }

    dependencies {
        compile "org.spongepowered:spongeapi:3.0.0";
        compile "shadow 'com.fasterxml.jackson.core:jackson-core:2.7.0'";
        compile "shadow 'com.fasterxml.jackson.core:jackson-annotations:2.7.0'";
        compile "shadow 'com.fasterxml.jackson.core:jackson-databind:2.7.0'";
    }

The configurations block should be above where it’s referenced. Put it above jar section.

Not quite, try this:

    compile "org.spongepowered:spongeapi:3.0.0"
    shadow "com.fasterxml.jackson.core:jackson-core:2.7.0"
    shadow "com.fasterxml.jackson.core:jackson-annotations:2.7.0"
    shadow "com.fasterxml.jackson.core:jackson-databind:2.7.0"

OK. Now I put it like this:

    configurations {
        shadow
        compile.extendsFrom shadow
    }
    dependencies {
        compile "org.spongepowered:spongeapi:3.0.0";
        shadow "com.fasterxml.jackson.core:jackson-core:2.7.0";
        shadow "com.fasterxml.jackson.core:jackson-annotations:2.7.0";
        shadow "com.fasterxml.jackson.core:jackson-databind:2.7.0";
    }
    jar {
        configurations.shadow.each { slice ->
            from(project.zipTree(slice)) {
            }
        }
    }

But NoClassDefFoundError is still thrown.

Don’t you need to include the gradle shadow plugin?

But anyway, I thought if you where shadowing jar’s it was good practice to rename them so you don’t conflict with others using the library?

I found a solution for my original problem using Gson.

Me too, GSON is so much simpler than everything.