无法在 Windows 上使用 kafka-run-class.bat 运行类



我使用 KafkaStream API 创建了一个演示应用程序。 尝试使用 kafka-run-class.bat 文件运行应用程序,但出现错误 "找不到或加载主类 com.kafka.StreamApp">

这是通往我类的路径: "C:\Users\ankit.srivastava\eclipse-workspace\kafka-demo\src\main\java\com\kafka">

我已将我的类路径环境变量设置为:

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

命令我正在尝试运行以从"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();
}

}

由于我的项目文件夹已添加到 CLASSPATH 变量批处理脚本中,因此应该找到该类并启动应用程序,但它给出错误
"找不到或加载主类 com.kafka.StreamApp">

你不需要 kafka-run-class 来运行你自己的 Consumer 或 Producer。您应该能够部署和运行代码,而无需依赖于在任何计算机上安装 Kafka。

话虽如此,您只会像往常一样使用java运行代码。

关于你的错误,它不是特定于卡夫卡的。简单地说,您已经将 CLASSPATH 指向了 Java 文件,而不是编译的类文件。

根据文件的路径,似乎您可能正在使用Maven或Gradle,因此我建议使用从这些文件构建的JAR文件

根据你之前的问题,我建议使用Spring-Kafka或Spring Cloud Streams来简化代码的配置。

相关内容

最新更新