user3421442 user3421442 - 2 days ago 4
Java Question

How to fix NoClassDefFoundError with custom Kafka producer under Eclipse?

I am trying to include Kafka module in my project.

I have added the following jars as external jar libraries in eclipse and have also update the build.xml to include the references to the jar:

  • kafka-clients-

  • kafka_2.10-

  • scala-library-2.10.4.jar

I wrote a sample Producer class

public class KafkaWriteRequestProducer extends Thread
private final String topic;
private final KafkaProducer<Integer, byte[]> producer;
private final WriteRequest writeRequest;

public KafkaWriteRequestProducer(String topic, WriteRequest writeRequest)
this.topic = topic;
this.writeRequest = writeRequest;

//Initialize the config for Kafka Producer
Properties props = new Properties();
props.put("bootstrap.servers", "localhost:9092");
props.put("", "KafkaProducer");
props.put("key.serialzer", "org.apache.kafka.common.serialization.IntegerSerializer");
props.put("value.serializer", "org.apache.kafka.common.serialization.ByteArraySerializer");

producer = new KafkaProducer<Integer, byte[]>(props);

I have ensured that Zookeeper and Kafka brokers are running before I started this project. However, I am seeing
when it is trying to instantiate the
new KafkaProducer()

Am I missing something obvious?


For completion sake, the reason why I was seeing the error was because I didn't have the jars specified in the classpath. Once, I added the jars to the classpath, it worked perfectly well.