package com.xmpp.util;import org.jivesoftware.smack.Chat;
import org.jivesoftware.smack.MessageListener;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.packet.Message;public class Test {
public Test(){
try{
// Create a connection to the igniterealtime.org XMPP server.
 XMPPConnection con = new XMPPConnection("test.com");
 // Connect to the server
 con.connect();
 // Most servers require you to login before performing other tasks.
 con.login("ejabberd", "123");
 // Start a new conversation with John Doe and send him a message.
 // Disconnect from the server
 con.disconnect();
}catch (XMPPException e){
System.out.println(e);
}
}
public static void main(String args[]){
Test t = new Test();
System.out.println("test");
}
}
控制台报错信息:Exception in thread "main" java.lang.IllegalStateException: Not connected to server.
at org.jivesoftware.smack.XMPPConnection.sendPacket(XMPPConnection.java:445)
at org.jivesoftware.smack.NonSASLAuthentication.authenticate(NonSASLAuthentication.java:69)
at org.jivesoftware.smack.XMPPConnection.login(XMPPConnection.java:212)
at com.xmpp.util.Test.<init>(Test.java:17)
at com.xmpp.util.Test.main(Test.java:36)
通过浏览器登录控制台没问题。
ejabberd@test
123