为什么Kafka消费者在启动时抛出“java.lang.NoClassDefFoundError:kafka / consumer / ConsumerConfig”?

时间:2015-08-11 09:44:47

标签: java scala maven apache-kafka

我正在尝试apache网站中给出的示例中的基本消费者代码。编译很好。但是在使用命令"workflow_settings": [ { "to_company": "54b925f61ebe95e86c8b4567", "approved_status": "false", "status": "", "assignees": [ { "user_id": "50e6c53cf8ae035c1b000001", "name": "Ravindra Chouhan", "email": "rchouhan@intellobuild.com", "status": "", "date": "" }, { "user_id": "5264be641ebe951e118b4568", "name": "Jackub Joe", "email": "jackub@gmail.com", "status": "", "date": "" } ] } } 运行时我得到了ClassNotFoundException:

[workflow_settings] => Array
(
    [0] => Array
        (
            [to_company] => 54b925f61ebe95e86c8b4567
            [approved_status] => false
            [status] =>
            [assignees] => Array
                (
                    [0] => Array
                        (
                            [user_id] => 50e6c53cf8ae035c1b000001
                            [name] => Ravindra Chouhan
                            [email] => rchouhan@intellobuild.com
                            [status] =>
                            [date] =>
                        )

                    [1] => Array
                        (
                            [user_id] => 5264be641ebe951e118b4568
                            [name] => Jackub Joe
                            [email] => jackub@gmail.com
                            [status] =>
                            [date] =>
                        )

                )
        )
)

源代码是

java -jar target/JarName.jar

我正在使用Maven进行依赖性解析。 pom.xml的依赖部分看起来像这样

Exception in thread "main" java.lang.NoClassDefFoundError: kafka/consumer/ConsumerConfig
    at com.package.ConsumerClass.functionConsumer(ConsumerClass.java:41)
    at com.package.App.main(App.java:26)
Caused by: java.lang.ClassNotFoundException: kafka.consumer.ConsumerConfig
    at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
    at java.net.URLClassLoader$1.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 sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:358)

我正在使用kafka_2.10-0.8.2.1版本。欢迎提出任何建议。

2 个答案:

答案 0 :(得分:1)

java -jar target/JarName.jarMain-Class 内部运行JarName.jar,但没有依赖关系,例如Kafka的依赖关系。由于您的应用程序确实需要类路径上的依赖项jar,您应该使用How can I create an executable JAR with dependencies using Maven?中所述的解决方案,或者自己构建类路径。

除了你为它开发一个客户端之外,它与Kafka没有任何关系。

P.S。考虑使用Scala和sbt(或者也许是gradle),你的生活变得如此简单。

答案 1 :(得分:0)

你为什么使用kafka.consumer.ConsumerConfig?你应该使用:

import org.apache.kafka.clients.consumer.KafkaConsumer;
import org.apache.kafka.clients.consumer.ConsumerRecords;
import org.apache.kafka.clients.consumer.ConsumerRecord;

KafkaConsumer<String, String> consumer = new KafkaConsumer
         <String, String>(props);