为了账号安全,请及时绑定邮箱和手机立即绑定

运行 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 关注
  • 258 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号