Uncaught server error: java.lang.NoClassDefFoundError implementing a custom provider for an external mysql database

Hi all,
I am using the latest keycloak server 21.1.1 and i am implementing a custom provider for communicating with an external database.
I have a factory for creating an DAO implementation from an external library. Inside the library is tested that the dao is successfully created. The dao implementation has injected a mysqldatasource.

I successfully find the custom provider and is successfully configured with the properties regarding the database.

I have confirmed that the datasource has been successfully created by the properties, but once i try to create the DAO via factory i get the following error “Uncaught server error: java.lang.NoClassDefFoundError:”.

I use the following dependencies:
org.keycloak:keycloak-server-spi:21.1.1
org.keycloak:keycloak-core:21.1.1
org.keycloak:keycloak-model-legacy:21.1.1
org.keycloak:keycloak-model-legacy-private:21.1.1

The issue is that the code for the external library is not included into the jar. What is the best approach to include the implementation into the jar?

you can either provide the jar dependency along with your custom code in the providers folder or include dependencies inside the jar.

I leave the solution i had with gradle 7.4.0 and keycloak version 21.1.1

build.gradle
plugins {
id(“com.github.johnrengelman.shadow”) version “7.1.2”
}

apply plugin: ‘com.github.johnrengelman.shadow’

shadowJar {

dependencies {

// list of the dependencies needs to be included in the fat jar
include(dependency(“com.xxxxx:$Version”))

}

}

docker file
in the gradlew command added the shadowJar
and copied the xxxxx-all.jar file to the binaries of the keycloak with the name xxxxx.jar in order to have the jar with the same name with the custom provider.

Thank you for your responses.

Any update here ? I am getting this error : Uncaught server error: java.lang.NoClassDefFoundError: com/messagebird/MessageBirdService.I created this file based on tania’s comment :slight_smile: not working. help help help, messagebird 6.1.2 and keycloak 22.0.5
my gradle :

plugins {
    id 'java'
    id 'com.github.johnrengelman.shadow' version '7.1.2' (gradle, 7.4)
}

group 'nl.company'
version = 'DUMMY_VERSION_THAT_ALWAYS_GETS_REPLACED_BY_CI'

tasks.register('setVersionToCommitSha') {
    doLast {
        String s = buildFile.getText().replaceFirst("version = '$version'", "version = '" + inputCommitSha + "'")
        buildFile.setText(s)
    }
}

sourceCompatibility = JavaVersion.VERSION_17
repositories {
    mavenCentral()
}

configurations {
    bundleLib
}

dependencies {
    implementation "org.keycloak:keycloak-server-spi-private:${keycloak}"
    implementation "org.keycloak:keycloak-server-spi:${keycloak}"
    implementation "org.keycloak:keycloak-services:${keycloak}"
    implementation "com.messagebird:messagebird-api:${messagebird}"

    testImplementation "org.junit.jupiter:junit-jupiter-api:${junit}"
    testRuntimeOnly "org.junit.jupiter:junit-jupiter-engine:${junit}"

}

jar {
    from {
        configurations.bundleLib.collect { it.isDirectory() ? it : zipTree(it) }
    }
    manifest {
        attributes(
                'Dependencies': "org.keycloak.keycloak-services"
        )
    }
}

shadowJar {

    dependencies {
        include(dependency("org.keycloak:keycloak-services:$keycloak")) // keycloak 22.0.5
        include(dependency("com.messagebird:messagebird-api:${messagebird}"))// messagebird 6.1.2
        include(dependency("org.keycloak:keycloak-server-spi-private:${keycloak}"))
        include(dependency("org.keycloak:keycloak-server-spi:${keycloak}"))
        include(dependency("org.keycloak:keycloak-services:${keycloak}"))
    }
}

Mine it worked having only the shadowJar and in the shadowJar the dependencies of yours, not keycloak’s

@tania
Hi, can you please help me out? I’ve tried shadowJar gradle plugin approach, but no luck!

Yes I could @jamilxt

Can you please share with me the gradle configuration snippet you’ve done?

Didn’t understand this part…can you please elaborate?

It would be better to share with me yours?
Mine is sothing like
shadowJar{
dependencies{
include (dependency(“name” ))
}
}

Here you go. In my case, Keycloak not finding MinioClient.

plugins {
    id 'java'
    id "com.diffplug.spotless" version "5.3.0"
    id "com.github.johnrengelman.shadow" version "7.1.2"
}

apply plugin: "com.github.johnrengelman.shadow"

group 'com.example.app.keycloak'
version = '0.0.2-SNAPSHOT'


java {
    sourceCompatibility = JavaVersion.VERSION_17
    targetCompatibility = JavaVersion.VERSION_17
    withSourcesJar()
}

repositories {
    mavenCentral()
}

dependencies {
    implementation "org.keycloak:keycloak-server-spi:$keycloakVersion"
    implementation "org.keycloak:keycloak-server-spi-private:$keycloakVersion"
    implementation "org.keycloak:keycloak-core:$keycloakVersion"
    implementation "org.keycloak:keycloak-services:$keycloakVersion"
    implementation "org.projectlombok:lombok:$lombokVersion"
    implementation "org.slf4j:slf4j-api:$slf4j"
    implementation "io.minio:minio:$minioClient"
    annotationProcessor "org.projectlombok:lombok:$lombokVersion"
}

shadowJar {
    dependencies {
        include(dependency("io.minio:minio:$minioClient"))
    }
}

spotless.java {
    eclipse()
    removeUnusedImports()
}

test {
    useJUnitPlatform()
}

tasks.withType(JavaCompile).configureEach { task ->
    {
        task.dependsOn "spotlessApply"
    }
}

Do you want to elaborate on your case, so that I better understand?

What version of keycloak do you use?

I am developing an extension for Keycloak to upload the profile picture of the user. Minio is the client I am using for storing the profile pictures.

I am using the version:
java: 17
gradle: 7.5.1
keycloak: 21.0.1

My case was about a custom provider. I have created a folder called keycloak/provider-binaries and I put my generated jar there. Whatever I have into this folder I put into /opt/keycloak/providers/
If you dont have the correct jar into the /opt/keycloak/providers/, keycloak won’t find the right packages

1 Like