簡體   English   中英

MQTT無法在Eclipse IDE中解析

[英]MQTT cannot be resolved in Eclipse IDE

我正在學習MQTT協議,並運行在Google上找到的以下代碼:

import org.eclipse.paho.client.mqttv3.MqttClient;
import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;

public class MQTT {

    public static void main(String[] args) {

        String topic        = "MQTT Examples";
        String content      = "Message from MqttPublishSample";
        int qos             = 2;
        String broker       = "tcp://iot.eclipse.org:1883";
        String clientId     = "JavaSample";
        MemoryPersistence persistence = new MemoryPersistence();

        try {
            MqttClient sampleClient = new MqttClient(broker, clientId, persistence);
            MqttConnectOptions connOpts = new MqttConnectOptions();
            connOpts.setCleanSession(true);
            System.out.println("Connecting to broker: "+broker);
            sampleClient.connect(connOpts);
            System.out.println("Connected");
            System.out.println("Publishing message: "+content);
            MqttMessage message = new MqttMessage(content.getBytes());
            message.setQos(qos);
            sampleClient.publish(topic, message);
            System.out.println("Message published");
            sampleClient.disconnect();
            System.out.println("Disconnected");
            System.exit(0);
        } catch(MqttException me) {
            System.out.println("reason "+me.getReasonCode());
            System.out.println("msg "+me.getMessage());
            System.out.println("loc "+me.getLocalizedMessage());
            System.out.println("cause "+me.getCause());
            System.out.println("excep "+me);
            me.printStackTrace();
        }
    }
}

但是,當我在ECLIPSE IDE上運行它時,出現一些錯誤: 錯誤

Exception in thread "main" java.lang.Error: Unresolved compilation problems: 
    MemoryPersistence cannot be resolved to a type
    MemoryPersistence cannot be resolved to a type
    MqttClient cannot be resolved to a type
    MqttClient cannot be resolved to a type
    MqttConnectOptions cannot be resolved to a type
    MqttConnectOptions cannot be resolved to a type
    MqttMessage cannot be resolved to a type
    MqttMessage cannot be resolved to a type
    MqttException cannot be resolved to a type

    at MQTT.main(MQTT.java:16)

我不明白為什么會有這些錯誤。

該錯誤意味着eclipse無法找到Paho MQTT類。 這很可能是因為您尚未將包含這些類的jar文件添加到類路徑中。

考慮到您提出問題的方式,我猜您甚至沒有從paho網站下載所需的jar文件。

上下載SRC或與Maven建築詳細信息可以在泛美衛生組織的網站上找到這里

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM