Home > Net >  java.lang.NoClassDefFoundError when trying to run gradle build
java.lang.NoClassDefFoundError when trying to run gradle build

Time:10-04

I am trying to run a java application from a Dockerfile. The Dockerfile looks like this:

FROM gradle:7.2-jdk17-alpine AS build

COPY --chown=gradle:gradle . /home/gradle/src
WORKDIR /home/gradle/src
RUN gradle build --no-daemon

FROM openjdk:17

EXPOSE 7070

RUN mkdir /app
COPY --from=build /home/gradle/src/build/libs/*.jar /app/app.jar

ENTRYPOINT ["java", "-jar", "/app/app.jar"]

The container builds the image, but when I try to run the container, I get an error:

Exception in thread "main" java.lang.NoClassDefFoundError: com/mongodb/client/MongoClients

Is there something wrong with my build.gradle?

plugins {
    id 'java'
}

repositories {
    mavenCentral()
}

dependencies {
    implementation 'io.javalin:javalin:4.6.4'
    implementation 'org.slf4j:slf4j-simple:1.8.0-beta4'
    implementation 'com.fasterxml.jackson.core:jackson-databind:2.13.3'
    implementation 'org.mongodb:mongodb-driver-sync:4.7.1'
    implementation 'io.jsonwebtoken:jjwt-api:0.11.5'
    implementation 'io.jsonwebtoken:jjwt-impl:0.11.5'
    implementation 'io.jsonwebtoken:jjwt-jackson:0.11.5'
    implementation 'com.fasterxml.jackson.datatype:jackson-datatype-jsr310:2.13.3'
    implementation 'org.apache.poi:poi:5.2.2'
    implementation 'org.apache.poi:poi-ooxml:5.2.2'
    testImplementation 'org.junit.jupiter:junit-jupiter-api:5.9.0'
    testImplementation 'org.mockito:mockito-core:4.7.0'
    testImplementation 'com.tngtech.archunit:archunit-junit5:0.23.1'
    testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.9.0'
}

test {
    useJUnitPlatform()
}

jar {
    manifest {
        attributes(
                'Main-Class': 'packagename.Application'
        )
    }
}

CodePudding user response:

After hours of googling I updated my build.gradle:

plugins {
    id 'java'
    id 'com.github.johnrengelman.shadow' version '7.1.2'
}

repositories {
    mavenCentral()
}

dependencies {
    implementation 'io.javalin:javalin:4.6.4'
    implementation 'org.slf4j:slf4j-simple:1.8.0-beta4'
    implementation 'com.fasterxml.jackson.core:jackson-databind:2.13.3'
    implementation 'org.mongodb:mongodb-driver-sync:4.7.1'
    implementation 'io.jsonwebtoken:jjwt-api:0.11.5'
    implementation 'io.jsonwebtoken:jjwt-impl:0.11.5'
    implementation 'io.jsonwebtoken:jjwt-jackson:0.11.5'
    implementation 'com.fasterxml.jackson.datatype:jackson-datatype-jsr310:2.13.3'
    implementation 'org.apache.poi:poi:5.2.2'
    implementation 'org.apache.poi:poi-ooxml:5.2.2'
    testImplementation 'org.junit.jupiter:junit-jupiter-api:5.9.0'
    testImplementation 'org.mockito:mockito-core:4.7.0'
    testImplementation 'com.tngtech.archunit:archunit-junit5:0.23.1'
    testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.9.0'
}

test {
    useJUnitPlatform()
}

jar {
    manifest {
        attributes(
                'Main-Class': 'packagename.Application',
                'Class-Path': '/libs/*.jar'
        )
    }
}

And to build the jar I know use ./gradlew --no-daemon shadowJar. That works for me. Hope this helps someone :)

  • Related