nodef found org.apache.kafka.common.serialization.stringserializer

Abhishek koserwal
1 min readApr 18, 2022

Exception when you are trying to deploy a Kafka Producer/Consumer application as a jar.

Problem

I encountered this error while I was writing: Keycloak: Event Listener SPI & Publish to Kafka

Example code Producer.java#L41

Here I defined the StringSerialized.class as a property for the Kafka Producer configuration.

This application is deployed as a jar to the Keycloak Server (SPI). The package is imported into the class. But, it failed to get resolved during the runtime.

The hacky solution is to reset the current thread context

Using Producer.java#L34

Thread.currentThread().setContextClassLoader(null);

isn’t recommended. Playing with classloader can cause unpredictable issues

Solution

To resolve this issue. Make your assembly to add the dependency while building the Jar.

<!-- Maven Assembly Plugin -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<finalName>Your-jar-name</finalName>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<appendAssemblyId>false</appendAssemblyId>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>

If still, you see the issue. Try adding

Class.forName("org.apache.kafka.common.serialization.stringserializer");

Conclusion

Thank you for reading it. I was able to resolve it by adding the maven assembly plugin. Hope this was useful.

--

--

Abhishek koserwal

#redhatter #opensource #developer #kubernetes #keycloak #golang #openshift #quarkus #spring