Skip to content
Advertisement

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.

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

I tried running some other commands like

[username@gcloud-instance ~]$ 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(ConfigDef.java:744)
        at org.apache.kafka.common.config.ConfigDef.parseValue(ConfigDef.java:490)
        at org.apache.kafka.common.config.ConfigDef.parse(ConfigDef.java:483)
        at org.apache.kafka.common.config.AbstractConfig.<init>(AbstractConfig.java:113)
        at org.apache.kafka.common.config.AbstractConfig.<init>(AbstractConfig.java:133)
        at org.apache.kafka.clients.producer.ProducerConfig.<init>(ProducerConfig.java:490)
        at org.apache.kafka.clients.producer.KafkaProducer.<init>(KafkaProducer.java:290)
        at org.apache.kafka.clients.producer.KafkaProducer.<init>(KafkaProducer.java:317)
        at org.apache.kafka.clients.producer.KafkaProducer.<init>(KafkaProducer.java:302)
        at SimpleProducer.main(SimpleProducer.java:28)

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

Advertisement

Answer

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

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement