坚持使用smack制作聊天客户端的第一步

时间:2017-07-19 11:38:36

标签: java xmpp smack

我开始在smack和eclipse上使用java构建一个聊天客户端,所以我开始编写以下代码:

import org.jivesoftware.smack.XMPPConnection;

public class A {

// Create a connection to the igniterealtime.org XMPP server.
    XMPPConnection connection = new XMPPConnection("myserver.com");
    // Connect to the server
    connection.connect();
    // Most servers require you to login before performing other tasks.
    connection.login("admin2", "123");
public static void main(String[] args) {
    // TODO Auto-generated method stub
     A a= new A();

}

}

但我收到以下两个错误:

Syntax error on token "connect", Identifier expected after this token

Syntax error on token ".", @ expected after this token

有人可以帮助我吗?

1 个答案:

答案 0 :(得分:2)

你不能把语句(比如 connection.connect())放出方法体。

尝试这样的事情:

public class A 
{
    public void start()
    {
        XMPPConnection connection = new XMPPConnection("myserver.com");
        // Connect to the server
        connection.connect();
        // Most servers require you to login before performing other tasks.
        connection.login("admin2", "123");
    }
    public static void main(String[] args) 
    {
        // TODO Auto-generated method stub
        A a= new A();
        a.start();
    }
}