快速启动VM中的ClassNotFoundException

时间:2018-03-09 12:04:53

标签: java

我是Java新手。抱歉重复的问题。我正在尝试执行Java jar,但面临classpath的问题。有人可以帮助我吗?

[cloudera@quickstart Desktop]$ ls -rlt Kafka_Consumer.jar
-rw-rw-r-- 1 cloudera cloudera 2227 Mar  9 03:28 Kafka_Consumer.jar

[cloudera@quickstart Desktop]$ java -cp Kafka_Consumer.jar org.abc.cde.KafkaConsumerTest
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/kafka/clients/consumer/KafkaConsumer
    at org.abc.cde.KafkaConsumerTest.main(KafkaConsumerTest.java:27)
Caused by: java.lang.ClassNotFoundException: org.apache.kafka.clients.consumer.KafkaConsumer
    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)
    ... 1 more

以下是我的Java代码段,没有任何错误:

 package org.abc.cde;

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

import java.util.Arrays;
import java.util.Properties;

public class KafkaConsumerTest {

    public static void main(String[] args) {

        //consumer properties
        Properties props = new Properties();
        props.put("bootstrap.servers", "localhost:9092");
        props.put("group.id", "test-group");

        //using auto commit
        props.put("enable.auto.commit", "true");

        //string inputs and outputs
        props.put("key.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
        props.put("value.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");

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

        //subscribe to topic
        consumer.subscribe(Arrays.asList("my-topic"));

        //infinite poll loop
        while (true) {
            ConsumerRecords<String, String> records = consumer.poll(100);
            for (ConsumerRecord<String, String> record : records)
                System.out.printf("offset = %d, key = %s, value = %s\n", record.offset(), record.key(), record.value());
        }

    }

}

请注意,没有编译时错误。

先谢谢。

0 个答案:

没有答案