Kafka Utils 类路径错误:org.apache.kafka.common.utils.Utils

Kafka Utils wrong classpath: org.apache.kafka.common.utils.Utils

我正在尝试制作一个非常简单的 Kafka Producer,目前正在关注 producer example 除了我的制作人没有分区程序 class。

将所需文件导出到 jar 后,我将它们传输到我的 Linux 图像并尝试 运行 它。

我得到这个异常:

Exception in thread "main" java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader.main(JarRsrcLoader.java:58)
Caused by: java.lang.NoClassDefFoundError: org/apache/kafka/common/utils/Utils
    at kafka.client.ClientUtils$$anonfun$parseBrokerList.apply(ClientUtils.scala:103)
    at kafka.client.ClientUtils$$anonfun$parseBrokerList.apply(ClientUtils.scala:102)
    at scala.collection.TraversableLike$$anonfun$map.apply(TraversableLike.scala:194)
    at scala.collection.TraversableLike$$anonfun$map.apply(TraversableLike.scala:194)
    at scala.collection.mutable.ResizableArray$class.foreach(ResizableArray.scala:60)
    at scala.collection.mutable.ArrayBuffer.foreach(ArrayBuffer.scala:44)
    at scala.collection.TraversableLike$class.map(TraversableLike.scala:194)
    at scala.collection.mutable.ArrayBuffer.map(ArrayBuffer.scala:44)
    at kafka.client.ClientUtils$.parseBrokerList(ClientUtils.scala:102)
    at kafka.producer.BrokerPartitionInfo.<init>(BrokerPartitionInfo.scala:32)
    at kafka.producer.async.DefaultEventHandler.<init>(DefaultEventHandler.scala:41)
    at kafka.producer.Producer.<init>(Producer.scala:60)
    at kafka.javaapi.producer.Producer.<init>(Producer.scala:26)
    at producers.HelloWorldProducer.main(HelloWorldProducer.java:20)
    ... 5 more
Caused by: java.lang.ClassNotFoundException: org.apache.kafka.common.utils.Utils
    at java.net.URLClassLoader.run(URLClassLoader.java:366)
    at java.net.URLClassLoader.run(URLClassLoader.java:355)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
    ... 19 more

查看 kafka jar 后,我发现 utils 现在是它自己的包,而不是位于 common

解决此问题的最佳方法是什么?

答案最终变得非常愚蠢......我需要改用 kafka-clients-0.8.2.0.jar