Skip to content

Executing simple kafka producer program in java

I’ve setup kafka in my google cloud instance, and was able to run the commands for producer and consumer creation successfully.

Now I’m trying to run this simple kafka producer code in java, however I’m not able to successfully execute the code after compilation.

For compilation,

sudo javac -classpath '/opt/kafka/libs/*' *.java

works perfectly and the SimpleProducer.class is generated.

However when I try the execution command, I get the below errors.

[[email protected] ~]$ java -cp '/opt/kafka/libs/*':. SimpleProducer
Error: Could not find or load main class SimpleProducer

I tried running some other commands like

[[email protected] ~]$ sudo java -cp '/opt/kafka/libs/*':. SimpleProducer sample-quickstart-topic
Exception in thread "main" org.apache.kafka.common.config.ConfigException: Invalid value org.apache.kafka.common.serializa-tion.StringSerializer for configuration key.serializer: Class org.apache.kafka.common.seriali
za-tion.StringSerializer could not be found.
        at org.apache.kafka.common.config.ConfigDef.parseType(
        at org.apache.kafka.common.config.ConfigDef.parseValue(
        at org.apache.kafka.common.config.ConfigDef.parse(
        at org.apache.kafka.common.config.AbstractConfig.<init>(
        at org.apache.kafka.common.config.AbstractConfig.<init>(
        at org.apache.kafka.clients.producer.ProducerConfig.<init>(
        at org.apache.kafka.clients.producer.KafkaProducer.<init>(
        at org.apache.kafka.clients.producer.KafkaProducer.<init>(
        at org.apache.kafka.clients.producer.KafkaProducer.<init>(
        at SimpleProducer.main(

But none seem to work. Any help would be appreciated, thanks.


Linked page has a typo in the serializers

Remove the hyphens from serializa-tion since they are not allowed in Java class names

Or use StringSerializer.class directly as the value rather than a string.

I still recommend that you use an IDE along with Maven/Gradle rather than editing and compiling via a terminal