0

I am getting a NoClassDefFoundError when I try to compile and run the Producer example that comes with Kafka. I want to know how to resolve the error discussed below?

Caveat: I am a C++/C# programmer who is Linux literate and starting to learn Java. I can follow instructions, but may well ask for some clarification along the way.

I have a VM sandbox from Hortonworks that is running a Red Hat appliance. On it I have a working kafka server and by following this tutorial I am able to get the desired Producer posting messages to the server.

Now I want to get down to writing my own code, but first I decided to make sure I can compile the example files that Kafka came with After a day of trial and error I just cannot seem to get this going.

here is what I am doing:

I am going to the directory where the example files are located and typing:

javac -cp $KCORE:$KCLIENT:$SCORE:. ./*.java

$KCORE:$KCLIENT:$SCORE resolve to the jars for the kafka core, kafka-client, and scala libraries respectively. everything returns just fine with no errors and places all the class files in the current directory; however, when I follow up with

javac -cp $KCORE:$KCLIENT:$SCORE:. Producer

I get a NoClassDefFoundError telling me the following Error screen shot

The code for the class is

package kafka.examples; import java.util.Properties; import kafka.producer.KeyedMessage; import kafka.producer.ProducerConfig; public class Producer extends Thread { private final kafka.javaapi.producer.Producer<Integer, String> producer; private final String topic; private final Properties props = new Properties(); public Producer(String topic) { props.put("serializer.class", "kafka.serializer.StringEncoder"); props.put("metadata.broker.list", "localhost:9092"); // Use random partitioner. Don't need the key type. Just set it to Integer. // The message is of type String. producer = new kafka.javaapi.producer.Producer<Integer, String>(new ProducerConfig(props)); this.topic = topic; } public void run() { int messageNo = 1; while(true) { String messageStr = new String("Message_" + messageNo); producer.send(new KeyedMessage<Integer, String>(topic, messageStr)); messageNo++; } } } 

Can anybody point me in the right direction to resolve this error? Do the classes need to go in different directories for some reason?

1 Answer 1

2

The package name is a part of the class name you need to supply on the command line:

javac -cp $KCORE:$KCLIENT:$SCORE:. kafka.examples.Producer 

Also, you should be standing in the root directory of your class hierarchy, which seems to be two directories up (you're currently standing in kafka/examples. Alternatively, you can use ../.. instead of . in the -cp argument to denote that the root is two directories up.

You might want to get familiar with using an IDE such as IntelliJ IDEA or Eclipse (and how to use libraries in those IDEs), as this will make development much easier. Props for doing things the hard way first, though, as you'll get a better understanding of how things are stitched together (an IDE will essentially figure all those console commands for you without you noticing).

Sign up to request clarification or add additional context in comments.

1 Comment

This answer is correct, consider adding this to the classpath in kafka-run-class.sh in the bin folder of kafka, as well.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.