我尝试使用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);
然而,当我尝试完全相同的配置,以及其他一切相同时,在另一个项目中,这是一个数据可视化软件的插件,我收到了这个错误:
.... // 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
在我说的第一个工作中,我正在使用&#34; mvn clean编译程序集:single&#34;,但在第二个中我为整个项目创建了一个jar文件。因为可视化软件需要jar文件来安装插件。因为每件事都是一样的(至少我找不到任何区别,我使用相同的代码)我想问题是关于构建项目的方式。这里发生了什么? &#34; mvn clean编译程序集有什么区别:single&#34;在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>
如果我使用如下的阴影插件:
<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>
它不起作用,因为我需要在我的插件中使用manifest和transform作为目标。如何将kafka类添加到IntelliJ创建的jar文件中以解决此问题(我不确定这是否可以解决)?
答案 0 :(得分:1)
我找到了一个更简单的解决方案。我改变了
kafkaProps.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG,"org.apache.kafka.common.serialization.StringSerializer");
对此
kafkaProps.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
然后,我的代码也作为预编译插件的一部分运行。
答案 1 :(得分:0)
在编译时kafka-clients-0.10.0.0.jar
可用,因此代码编译成功,但在运行时它会丢失,因此会出现错误。
您有两个选择: