2017-10-22 13 views
0

で解決することができない私は、MQTTプロトコルについて学んでいると私は、私はGoogleで見つけ、このコードを実行します。MQTTは、Eclipse IDE

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上でそれを実行すると、いくつかのエラーが表示されます。 Error

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) 

なぜこれらのエラーがあるのか​​分かりません。

答えて

0

エラーは、eclipseがPaho MQTTクラスを見つけることができないことを意味します。これは、これらのクラスを含むjarファイルをクラスパスに追加していないためです。

質問をしたところで、私はあなたがpahoサイトから必要なjarファイルをダウンロードしていないと推測します。

mavenを使用してsrcまたは建物をダウンロードする方法の詳細は、Pahoサイトhere

にあります。