Kafka Producer Class未找到例外

Jol*_*ger 6 java maven apache-kafka

我尝试使用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);
Run Code Online (Sandbox Code Playgroud)

然而,当我尝试完全相同的配置,以及其他一切相同,在另一个项目,这是一个数据可视化软件的插件,我得到这个错误:

.... // 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
Run Code Online (Sandbox Code Playgroud)

在我说它工作的第一个,我使用"mvn clean compile assembly:single",但在第二个我为整个项目创建了一个jar文件.因为可视化软件需要jar文件来安装插件.因为每件事都是一样的(至少我找不到任何区别,我使用相同的代码)我想问题是关于构建项目的方式.这里发生了什么?"mvn clean compile assembly:single"和在IntelliJ中构建jar文件有什么区别?为什么我收到此错误以及如何解决此问题?非常感谢您的帮助!


正如我在第一个答案的最后一个评论中所说,我有一个插件,它已经显示并转换为目标.这里:

<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>
Run Code Online (Sandbox Code Playgroud)

如果我使用如下的阴影插件:

<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>
Run Code Online (Sandbox Code Playgroud)

它不起作用,因为我需要在我的插件中使用manifest和transform作为目标.如何将kafka类添加到IntelliJ创建的jar文件中以解决此问题(我不确定这是否可以解决)?

jrt*_*ell 0

问题是什么

在编译时kafka-clients-0.10.0.0.jar可用,因此代码可以成功编译,但在运行时它丢失,因此会出现错误。

如何修复它

您有 2 个选择:

  • 将 kafka JAR 包含在您的 JAR 中
  • 将 kafka JAR 添加到插件的类路径中