java kafka使用者noclassdeffounderror

mcvgt66p  于 2021-06-07  发布在  Kafka
关注(0)|答案(1)|浏览(334)

我正在学习使用测试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文件时需要注意的事情?

fbcarpbf

fbcarpbf1#

因为我无法查看整个项目,所以我会尝试这样做:右键单击项目->转到maven 2工具->单击生成工件(检查更新)。应该会创建任何缺少的依赖项。同时也要确保你查看了其他类似的帖子,这些帖子可能会像这样解决你的问题。

相关问题