Class StringSerializer.class could not be found

Hello community,

I would like to ask the question from the Running Command issue gradlew for producer & consumer issue, as there’s no solution mentioned and the issue is closed.

In my case the issue is exactly the same as the one mentioned above, with the only exception that I run the build on Amazon Linux 2023 machine.

@aaditya2000, could you share your solution?

Any help with actual solution would be highly appreciated.
Thanks

Without seeing your Gradle file, it’ll be difficult to help much, but similar to Maven, your jar may not have the necessary classpath or dependencies included (i.e kafka-clients)

Please try using gradle shadow plugin to create a shaded fat jar, then run that with Java, not gradlew

I struggled with this same issue for a long time, trying to use WSL on my desktop, then Amazon Linux in EC2, enabling debug logs, modifying the gradle config to point to a specific Java version, updating JAVA_HOME, outputting the classpath for analysis, setting the thread’s context class loader to null, and adding code to reference and use StringSerializer.class to ensure that it could actually be picked up by the ClassLoader.

None of it made any difference. What finally resolved the issue was changing the following:

// THIS FAILS
        producerConfigs.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, "StringSerializer.class");
        producerConfigs.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, "KafkaProtobufSerializer.class");

// THIS WORKS
        producerConfigs.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, "org.apache.kafka.common.serialization.StringSerializer");
        producerConfigs.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, "io.confluent.kafka.serializers.protobuf.KafkaProtobufSerializer");

I hope that this is helpful to someone else.