Wassadamo
Wassadamo

Reputation: 1396

Error Initializing Kafka Producer with Avro Serializer: java.lang.NoClassDefFoundError

I'm having trouble getting a KafkaProducer instantiation to accept an avro serializer for the value serializer config property. I installed kafka-avro-serializer-5.2.2.jar with

mvn install:install-file -DgroupId=io.confluent -DartifactId=kafka-avro-serializer \
    -Dversion=5.2.2 -Dpackaging=jar -Dfile=C:/Users/myuser/Downloads/kafka-avro-serializer-5.2.2.jar

The jar and related files were added to ~\.m2\repository\io\confluent\kafka-avro-serializer\5.2.2. I then added the following to my project POM file within dependencies:

<dependency>
    <groupId>io.confluent</groupId>
    <artifactId>kafka-avro-serializer</artifactId>
    <version>5.2.2</version>
</dependency>

Then the following imports in my client work fine; code compiles

import io.confluent.kafka.serializers.KafkaAvroSerializer;
import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.Producer;
import org.apache.kafka.clients.producer.ProducerConfig;

But running this:

Properties props = new Properties();
props.put("bootstrap.servers", "localhost:9092");
props.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName());
props.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, KafkaAvroSerializer.class.getName());

final Producer<String, Object> producer = new KafkaProducer<>(props);

Results in error:

Exception in thread "main" java.lang.NoClassDefFoundError: io/confluent/common/config/ConfigException

I found a similar issue tracked here. But I'm not using Kafka Connect. Also

Upvotes: 0

Views: 5925

Answers (2)

Wassadamo
Wassadamo

Reputation: 1396

I solved my issue by installing additional jar files from confluent: common-config, common-utils, common-utils, and kafka-schema-registry-client.

e.g.

$ mvn install:install-file -DgroupId=io.confluent -DartifactId=common-utils -Dversion=5.2.2 -Dpackaging=jar -Dfile=C:\Users\myuser\Documents\Jars\common-utils-5.2.2.jar

Properties filled by renaming .jar -> .jar.zip, Open Archive, navigate to META-INF/maven/.../pom.properties

Then update project POM in a similar fashion.

Upvotes: 0

Kris
Kris

Reputation: 8873

You have to add one more dependency for common config.

<dependency>
    <groupId>io.confluent</groupId>
    <artifactId>common-config</artifactId>
    <version>5.2.2</version>
</dependency>

The class is available in this JAR.

Upvotes: 2

Related Questions