简体   繁体   English

未找到卡夫卡生产者类异常

[英]Kafka Producer Class Not Found Exception

I tried to implement a simple producer consumer example with kafka and I achieved with the following properties:我尝试使用 kafka 实现一个简单的生产者消费者示例,并使用以下属性实现:

Properties configProperties = new Properties();
    configProperties.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG,"localhost:" + portNumber);
    configProperties.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG,"org.apache.kafka.common.serialization.ByteArraySerializer");
    configProperties.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG,"org.apache.kafka.common.serialization.StringSerializer");

    // Belirtilen property ayarlarına sahip kafka producer oluşturulur
    org.apache.kafka.clients.producer.Producer producer = new KafkaProducer(configProperties);

However when I try the exact same configs, and everything else the same, in another project which is a plugin for a data visualization software, I got this error:但是,当我在另一个数据可视化软件插件项目中尝试完全相同的配置并且其他所有内容都相同时,我收到此错误:

.... // Here there is some other stuff but I thing the important one is the below one
Caused by: java.lang.NoClassDefFoundError: org/apache/kafka/clients/producer/Producer
at App.MyControlPanel.<init>(MyControlPanel.java:130)
at App.CytoVisProject.<init>(CytoVisProject.java:29)
... 96 more
Caused by: java.lang.ClassNotFoundException: org.apache.kafka.clients.producer.Producer
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 98 more

In the first one that I said it worked, I was using "mvn clean compile assembly:single", but in the second one I created a jar file for the whole project.在我说它有效的第一个中,我使用了“mvn clean compile assembly:single”,但在第二个中我为整个项目创建了一个 jar 文件。 Because the visualization software wants a jar file to install the plugin.因为可视化软件要一个jar文件来安装插件。 Since every thing is same (At least I could not find any difference, I used same code) I guess the problem is about the way build the project.由于每件事都是相同的(至少我找不到任何区别,我使用了相同的代码)我想问题在于构建项目的方式。 What happened here?这里发生了什么? What is the difference between "mvn clean compile assembly:single" and building a jar file in IntelliJ? “mvn clean compile assembly:single”和在 IntelliJ 中构建 jar 文件有什么区别? Why I got this error and how to fix this?为什么我收到这个错误以及如何解决这个问题? Thanks a lot for help!非常感谢您的帮助!


As I said in the last comment of the first answer, I have a plugin which has manifest and transform as goal.正如我在第一个答案的最后一条评论中所说,我有一个插件,它以 manifest 和 transform 为目标。 Here:这里:

<plugin>
            <groupId>com.springsource.bundlor</groupId>
            <artifactId>com.springsource.bundlor.maven</artifactId>
            <version>1.0.0.M2</version>
            <configuration>
                <outputManifest>C:\Users\USER\AppData\Local\Temp\archetype2tmp/META-INF/MANIFEST.MF</outputManifest>
                <failOnWarnings>false</failOnWarnings>
                <removeNullHeaders>true</removeNullHeaders>
                <manifestHeaders><![CDATA[Bundle-ManifestVersion: 2
Bundle-Name: CytoVisProject
Bundle-SymbolicName: CytoVisProject
Spring-DM-Version: ${pom.version}
]]></manifestHeaders>
            </configuration>
            <!-- generate the manifest automatically during packaging -->
            <executions>
                <execution>
                    <id>bundle-manifest</id>
                    <phase>package</phase>
                    <goals>
                        <goal>manifest</goal>
                        <goal>transform</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

If I use a shade plugin like below:如果我使用如下所示的阴影插件:

<plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            <version>3.1.0</version>
            <configuration>

            </configuration>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>shade</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

It does not works, because I need to use manifest and transform as goals in my plugin.它不起作用,因为我需要在我的插件中使用清单和转换作为目标。 How can I add kafka classes to the jar file that IntelliJ creates to solve this problem (I am not sure if this can solve or not)?如何将 kafka 类添加到 IntelliJ 创建的 jar 文件以解决此问题(我不确定这是否可以解决)?

I've found an easier solution.我找到了一个更简单的解决方案。 I have changed我变了

kafkaProps.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG,"org.apache.kafka.common.serialization.StringSerializer");

to this对此

kafkaProps.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class);

And afterwards my code run, also as part of a pre-compiled plug-in.然后我的代码运行,也作为预编译插件的一部分。

What the problem is问题是什么

At compile time kafka-clients-0.10.0.0.jar is available, so the code compiles successfully, but at runtime it is missing, hence the error you get.在编译时kafka-clients-0.10.0.0.jar可用,所以代码编译成功,但在运行时它丢失了,因此你得到错误。

How to fix it如何修复

You have 2 options:您有 2 个选择:

  • Include the kafka JAR inside your JAR在 JAR 中包含 kafka JAR
  • Add the kafka JAR to your plugin's classpath将 kafka JAR 添加到插件的类路径

另外,请确保您没有对标有提供范围的 kafka 客户端的依赖项。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM