Why am I getting "java.lang.NoClassDefFoundError" despite referencing the library in eclipse and specifying the dependency in build.gradle?

Viewed 38

The error gets thrown for org/apache/commons/compress/archivers/tar/TarArchiveInputStream as seen here.

I have referenced this library in eclipse (as seen here)

The dependency has also been specified in build.gradle. The contents of the gradle file:

plugins {
    id 'org.spongepowered.plugin' version '0.9.0'
}

group = pluginGroup
version = pluginVersion

sourceCompatibility = '1.8'
targetCompatibility = '1.8'

dependencies {
    compileOnly 'org.spongepowered:spongeapi:7.2.0'
    annotationProcessor 'org.spongepowered:spongeapi:7.2.0'
    compile 'org.apache.commons:commons-compress:1.21'
}

sponge.plugin.id = pluginId

Eclipse also does not flag any errors. Lines of code such as tarInput.getNextTarEntry() appear in the correct colour with no red underlining, and eclipse even autocompletes the names of methods found in commons-compress.

Given eclipse seems to be working correctly with the dependency, and given that running ./gradlew build leads to a successful build, I am therefore at a loss for why I am getting the java.lang.NoClassDefFoundError error.

1 Answers

@nitind helped. I focused upon runtime and found the problem was to do with my build.gradle file (even though the build was successful!). I changed it to this and the error has vanished:

buildscript {
        repositories {
                mavenCentral()
                }
}

plugins {
    id 'org.spongepowered.plugin' version '0.9.0'
}

apply plugin: 'java'

group = pluginGroup
version = pluginVersion

sourceCompatibility = '1.8'
targetCompatibility = '1.8'

dependencies {
    compileOnly 'org.spongepowered:spongeapi:7.2.0'
    annotationProcessor 'org.spongepowered:spongeapi:7.2.0'
    compile 'org.apache.commons:commons-compress:1.21'
}

jar {
        from configurations.compile.collect { zipTree it }
}

sponge.plugin.id = pluginId
Related