Kafka Consumer Example抛出ClassNotFoundException

时间:2014-04-11 02:56:03

标签: java scala maven noclassdeffounderror

我使用Scala 2.8构建了Kafka 0.8;从this site开始的步骤。我还可以成功运行kafka-console-producer和kafka-console-consumer示例。我现在正在试验Consumer Group Example(请参阅"完整源代码"部分)。我得到的例外如下:

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:622)
    at org.codehaus.mojo.exec.ExecJavaMojo$1.run(ExecJavaMojo.java:297)
    at java.lang.Thread.run(Thread.java:701)
Caused by: java.lang.NoClassDefFoundError: scala/Tuple2$mcJJ$sp
    at kafka.consumer.ConsumerConfig.<init>(Unknown Source)
    at ConsumerGroupExample.createConsumerConfig(ConsumerGroupExample.java:55)
    at ConsumerGroupExample.<init>(ConsumerGroupExample.java:18)
    at ConsumerGroupExample.main(ConsumerGroupExample.java:64)
    ... 6 more
Caused by: java.lang.ClassNotFoundException: scala.Tuple2$mcJJ$sp
    at java.net.URLClassLoader$1.run(URLClassLoader.java:217)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:323)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:268)
    ... 10 more

我的依赖关系如下所示:

<dependencies>
  <dependency>
  <groupId>junit</groupId>
  <artifactId>junit</artifactId>
  <version>3.8.1</version>
  <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.apache.kafka</groupId>
    <artifactId>kafka_2.10</artifactId>
    <version>0.8.0</version>
</dependency>
<dependency>
    <groupId>javax.inject</groupId>
    <artifactId>javax.inject</artifactId>
    <version>1</version>
</dependency>
<dependency>
    <groupId>org.scala-lang</groupId>
    <artifactId>scala-library</artifactId>
    <version>2.8.0</version>
</dependency>
<dependency>
    <groupId>log4j</groupId>
    <artifactId>log4j</artifactId>
    <version>1.2.17</version>
</dependency>
<dependency>
    <groupId>com.101tec</groupId>
    <artifactId>zkclient</artifactId>
    <version>0.3</version>
</dependency>
<dependency>
    <groupId>com.yammer.metrics</groupId>
    <artifactId>metrics-core</artifactId>
    <version>2.2.0</version>
</dependency>

作为一个说明,我可以使用Python client运行它。但我真的需要在 Java 中完成这项工作。非常感谢任何帮助!

1 个答案:

答案 0 :(得分:2)

您正在使用针对2.10构建的kafka(请参阅工件名称中的_2.10)

<dependency>
    <groupId>org.apache.kafka</groupId>
    <artifactId>kafka_2.10</artifactId>
    <version>0.8.0</version>
</dependency>

使用scala 2.8.0

<dependency>
    <groupId>org.scala-lang</groupId>
    <artifactId>scala-library</artifactId>
    <version>2.8.0</version>
</dependency>

主要的scala版本(2.8。*,2.9。*,2.10。*)彼此之间不兼容 - 选择正确版本的kafka工件或项目的scala库,一切都会好的。< / p>