ANKIT SRIVASTAVA
ANKIT SRIVASTAVA

Reputation: 173

Not able to run a class using kafka-run-class.bat on windows

I have created a demo app using KafkaStream API. Trying to run the app using kafka-run-class.bat file but getting error "Could not find or load main class com.kafka.StreamApp"

This is the path to my class : "C:\Users\ankit.srivastava\eclipse-workspace\kafka-demo\src\main\java\com\kafka"

I have set my CLASSPATH envrionment variable as :

"C:\Users\ankit.srivastava\eclipse-workspace\kafka-demo\src\main\java"

Command i am trying to run to start the app from "C:\Users\ankit.srivastava\Documents\Kafka\kafka" :

"bin\windows\kafka-run-class.bat com.kafka.StreamApp"

public class StreamApp {


public static void main(String[] args) {

    Properties props = new Properties();
    props.put(StreamsConfig.APPLICATION_ID_CONFIG, "wordcount-application");
    props.put(StreamsConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9092");
    props.put(StreamsConfig.DEFAULT_KEY_SERDE_CLASS_CONFIG, Serdes.String().getClass());
    props.put(StreamsConfig.DEFAULT_VALUE_SERDE_CLASS_CONFIG, Serdes.String().getClass());

    StreamsBuilder builder = new StreamsBuilder();
    KStream<String, String> textLines = builder.stream("TextLinesTopic");
    KTable<String, Long> wordCounts = textLines
        .flatMapValues(textLine -> Arrays.asList(textLine.toLowerCase().split("\\W+")))
        .groupBy((key, word) -> word)
        .count(Materialized.<String, Long, KeyValueStore<Bytes, byte[]>>as("counts-store"));
    wordCounts.toStream().to("WordsWithCountsTopic", Produced.with(Serdes.String(), Serdes.Long()));

    KafkaStreams streams = new KafkaStreams(builder.build(), props);
    streams.start();
}

}

Since my project folder is added to the CLASSPATH variable batch script should have found the class and started the app butits giving an error
"Could not find or load main class com.kafka.StreamApp"

Upvotes: 0

Views: 965

Answers (1)

OneCricketeer
OneCricketeer

Reputation: 191758

You don't need kafka-run-class to run your own Consumer or Producer. You should be able to deploy and run your code without depending on having Kafka installed on any machine.

That being said, you'd run the code just using java, as normal.

Regarding your error, it's not specific to Kafka. Simply, you've pointed the CLASSPATH at Java files, not compiled class files.

Based on the path of files, seems like you might be using Maven or Gradle, so I'd suggest using the JAR file built from those

And based on your previous questions, I'd suggest using Spring-Kafka or Spring Cloud Streams to simplify the configuration of your code

Upvotes: 1

Related Questions