0

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.

4
  • This means that the class is available during the compilation but it is not present during runtime. Are you using any build tool? Commented Jan 13, 2022 at 8:07
  • Build tool like? I've installed kafka and jdk. Plus I've specified the classpath just like in the link, confused as to why this isn't working. Commented Jan 13, 2022 at 8:09
  • 1
    Check the classes in the classpath, and also confirm that it is serializa-tion. I believe it should be serialization that '-' is a typo. Commented Jan 13, 2022 at 8:17
  • 1
    I suggest you use Maven/Gradle and run jar files, not compiled classes or use the Kafka server classpath, which includes more files than you actually need. Besides that, do you get the same error on kafka-console-producer? If not, your classpath is incorrect Commented Jan 13, 2022 at 16:00

1 Answer 1

1

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

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

1 Comment

Thanks mate, I feel stupid for not seeing that typo. And yes, I'll have a look at Maven/Gradle and how I can run from an IDE. Thanks again :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.