Sushmita Bhattacharya Sushmita Bhattacharya - 6 months ago 20
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(ConsumerClass.java:41)
at com.package.App.main(App.java:26)
Caused by: java.lang.ClassNotFoundException: kafka.consumer.ConsumerConfig
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)


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("zk.connectiontimeout.ms", "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();
while(it.hasNext())
System.out.println(new String(it.next().message()));
}
}


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

<dependency>
<groupId>org.apache.kafka</groupId>
<artifactId>kafka_2.10</artifactId>
<version>0.8.2.1</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.apache.kafka</groupId>
<artifactId>kafka-clients</artifactId>
<version>0.8.2.1</version>
<scope>compile</scope>
</dependency>


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

Answer

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.