Sushmita Bhattacharya Sushmita Bhattacharya - 4 months ago 11x
Java Question

Why does Kafka consumer throw "java.lang.NoClassDefFoundError: kafka/consumer/ConsumerConfig" at startup?

I am trying out the basic consumer code from example given in apache website. Compilation is fine. But I am getting ClassNotFoundException while running using the command

java -jar target/JarName.jar

Exception in thread "main" java.lang.NoClassDefFoundError: kafka/consumer/ConsumerConfig
at com.package.ConsumerClass.functionConsumer(
at com.package.App.main(
Caused by: java.lang.ClassNotFoundException: kafka.consumer.ConsumerConfig
at Method)
at java.lang.ClassLoader.loadClass(
at sun.misc.Launcher$AppClassLoader.loadClass(
at java.lang.ClassLoader.loadClass(

The source code is

package com.package;
import java.util.HashMap;
import java.util.Properties;
import java.util.Map;
import java.util.List;
import java.lang.Object;
import kafka.consumer.Consumer;
import kafka.consumer.ConsumerConfig;
import kafka.consumer.ConsumerConnector;
import kafka.consumer.ConsumerIterator;
import kafka.consumer.KafkaStream;
public class ExampleConsumer {
public void exampleConsumer(){
Properties props = new Properties();
props.put("zk.connect", "localhost:2181");
props.put("", "1000000");
props.put("groupid", "test_group");

ConsumerConfig consumerConfig = new ConsumerConfig(props);
ConsumerConnector consumerConnector = (ConsumerConnector) Consumer.createJavaConsumerConnector(consumerConfig);

Map<String, Object> topicCountMap = new HashMap<String, Object>();
topicCountMap.put("test", new Integer(1));
Map<String, List<KafkaStream<byte[], byte[]>>> consumerMap = (Map<String, List<KafkaStream<byte[], byte[]>>>) consumerConnector.createMessageStreams((scala.collection.Map<String, Object>) topicCountMap);
KafkaStream<byte[], byte[]> stream = consumerMap.get("test").get(0);
ConsumerIterator<byte[], byte[]> it = stream.iterator();
System.out.println(new String(;

I am using Maven for dependency resolution. The dependency part of the pom.xml looks like this


I am using kafka_2.10- version. Any suggestion is welcome.


java -jar target/JarName.jar runs Main-Class inside JarName.jar without dependencies like Kafka's ones. Since your application does need dependencies jars on classpath you should use a solution as explained in How can I create an executable jar with dependencies using Maven? or build up the classpath yourself.

BTW It has nothing to do with Kafka except that you develop a client for it.

p.s. Consider using Scala and sbt (or perhaps gradle) and your life becomes so much easier.