亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

運行 Paho 客戶端 MQTT 的訂閱者時未找到類異常

運行 Paho 客戶端 MQTT 的訂閱者時未找到類異常

呼如林 2022-08-03 15:41:01
我正在嘗試使用 org.eclipse.paho.client.mqttv3-1.2.0 中的軟件包實現 Pub-Sub.jar在用戶代碼下方:import org.eclipse.paho.client.mqttv3.MqttClient;import org.eclipse.paho.client.mqttv3.MqttTopic;import org.eclipse.paho.client.mqttv3.MqttCallback;import org.eclipse.paho.client.mqttv3.MqttMessage;import org.eclipse.paho.client.mqttv3.MqttException;import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken;import java.net.*;public class Subscriber {  //public static final String BROKER_URL = "tcp://iot.eclipse.org:1883";  public static final String BROKER_URL = "tcp://localhost:1883";  private MqttClient client;  String topic = "Demo Topic";  public static void main(String[] args) {    System.out.println("MQTT Broker: " + BROKER_URL);    new Subscriber();  }  public Subscriber() {    String clientId = getMacAddress() + "-sub";    System.out.println("Client ID = " + clientId);    try   {      client = new MqttClient(BROKER_URL, clientId);      client.connect();      client.setCallback(new SubscribeCallback());      client.subscribe(topic);    } catch (MqttException e) {      e.printStackTrace(); System.exit(1);    }  }  public byte[] getMacAddress(){    byte[] mac = new byte[6];    try{      InetAddress address = InetAddress.getLocalHost();      NetworkInterface nwi = NetworkInterface.getByInetAddress(address);      mac = nwi.getHardwareAddress();      System.out.println(mac);    } catch(Exception e) {      System.out.println(e);    }    return mac;  }}class SubscribeCallback implements MqttCallback {  @Override  public void connectionLost (Throwable cause) { }  @Override  public void messageArrived (String topic, MqttMessage message) {    System.out.println("Message arrived. Topic: " + topic    + " Message: " + message.toString());    if ("I'm gone".equals(topic)) {      System.out.println("Sensor gone!");    }  }  @Override  public void deliveryComplete (IMqttDeliveryToken token) { }
查看完整描述

1 回答

?
隔江千里

TA貢獻1906條經驗 獲得超10個贊

您還需要將當前目錄添加到類路徑中

java -cp .:org.eclipse.paho.client.mqttv3-1.2.0.jar Subscriber

如果在窗口上運行,則更改 for。:;


查看完整回答
反對 回復 2022-08-03
  • 1 回答
  • 0 關注
  • 231 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號