Java Kafka使用者noClassDefFoundError

时间:2018-12-13 15:39:05

标签: java apache-kafka noclassdeffounderror

我正在使用测试的Kafka消费者和生产者进行学习,但是面临以下错误。

Kafka消费者计划:

package kafka001;

import java.util.Arrays;
import java.util.Properties;
import java.util.Scanner;
import org.apache.kafka.clients.consumer.*;
import org.apache.kafka.common.errors.WakeupException;

public class ConsumerApp {
    private static Scanner in;
    private static boolean stop = false;

    public static void main(String[] args) throws Exception {
        System.out.println(args[0] + args.length);
        if (args.length != 2) {
            System.err.printf("Usage: %s <topicName> <groupId>\n");
            System.exit(-1);

        }

        in = new Scanner(System.in);
        String topicName = args[0];
        String groupId = args[1];
        ConsumerThread consumerRunnable = new ConsumerThread(topicName, groupId);
        consumerRunnable.start();
        //System.out.println("Here");   
        String line = "";
        while (!line.equals("exit")) {
            line = in.next();
        }
        consumerRunnable.getKafkaConsumer().wakeup();
        System.out.println("Stopping consumer now.....");
        consumerRunnable.join();

    }

    private static class ConsumerThread extends Thread{
        private String topicName;
        private String groupId;
        private KafkaConsumer<String,String> kafkaConsumer;
        public ConsumerThread(String topicName, String groupId){
            //System.out.println("inside ConsumerThread constructor");  
            this.topicName = topicName;
            this.groupId = groupId;
        }

        public void run() {
            //System.out.println("inside run"); 
            // Setup Kafka producer properties
            Properties configProperties = new Properties();
            configProperties.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, "aup7727s.unix.anz:9092");
            configProperties.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, "org.apache.kafka.common.serialization.StringDeserializer");
            configProperties.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, "org.apache.kafka.common.serialization.StringDeserializer");
            configProperties.put(ConsumerConfig.GROUP_ID_CONFIG, groupId);
            configProperties.put(ConsumerConfig.CLIENT_ID_CONFIG, "simple");

            // subscribe to topic
            kafkaConsumer = new KafkaConsumer<String, String>(configProperties);
            kafkaConsumer.subscribe(Arrays.asList(topicName));

            // Get/process messages from topic and print it to console
            try {while(true) {
                ConsumerRecords<String, String> records = kafkaConsumer.poll(100);
                for (ConsumerRecord<String, String> record : records)
                    System.out.println(record.value());

            }
          } catch(WakeupException ex) {
              System.out.println("Exception caught " + ex.getMessage());
          }finally {
              kafkaConsumer.close();
              System.out.println("After closing KafkaConsumer");
          }

        }

        public KafkaConsumer<String,String> getKafkaConsumer(){
            return this.kafkaConsumer;
         }


    }
}

编译代码时,我注意到以下类文件:

ConsumerApp $ ConsumerThread.class和 ConsumerApp.class

我已经通过eclipse生成了一个名为ConsumerApp.jar的jar文件,当我在Hadoop集群中运行它时,出现如下所示的noclassdeffound错误:

java -cp ConsumerApp.jar kafka001/ConsumerApp console1 group1
or
hadoop jar ConsumerApp.jar console1 group1

Exception in thread "main" java.lang.NoClassDefFoundError: org.apache.kafka.common.errors.WakeupException
        at kafka001.ConsumerApp.main(ConsumerApp.java:24)
Caused by: java.lang.ClassNotFoundException: org.apache.kafka.common.errors.WakeupException
        at java.net.URLClassLoader.findClass(URLClassLoader.java:607)
        at java.lang.ClassLoader.loadClassHelper(ClassLoader.java:846)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:825)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:325)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:805)
        ... 1 more

我正在使用Eclipse进行编译,maven构建和生成jar文件。第24行对应于ConsumerThread实例的创建。

我无法解决是否由于ConsumerThread类名称的错误保存(生成为ConsumerApp $ ConsumerThread.class而不是ConsumerThread.class的类文件)?或生成jar文件时需要注意的事情?

1 个答案:

答案 0 :(得分:0)

由于我无法查看整个项目,因此我将尝试以下操作:右键单击项目->转到Maven 2工具->单击生成工件(检查更新)。那应该创建任何丢失的依赖项。另外,请确保您查看其他可能解决您问题的类似帖子,例如this