AnsweredAssumed Answered

Smack examples from the net do not work for me?

Question asked by Ace on Jul 12, 2016

Hello,

I have been trying to run this simple examples from the net.

 

import java.util.*;
import java.io.*;

import org.jivesoftware.smack.Chat;
import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.MessageListener;
import org.jivesoftware.smack.Roster;
import org.jivesoftware.smack.RosterEntry;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.packet.Message;

public class JabberSmackAPI implements MessageListener{

    XMPPConnection connection;

    public void login(String userName, String password) throws XMPPException
    {
    ConnectionConfiguration config = new ConnectionConfiguration("im.server.here",5222, "Work");
    connection = new XMPPConnection(config);

    connection.connect();
    connection.login(userName, password);
    }

    public void sendMessage(String message, String to) throws XMPPException
    {
    Chat chat = connection.getChatManager().createChat(to, this);
    chat.sendMessage(message);
    }

    public void displayBuddyList()
    {
    Roster roster = connection.getRoster();
    Collection<RosterEntry> entries = roster.getEntries();

    System.out.println("\n\n" + entries.size() + " buddy(ies):");
    for(RosterEntry r:entries)
    {
    System.out.println(r.getUser());
    }
    }

    public void disconnect()
    {
    connection.disconnect();
    }

    public void processMessage(Chat chat, Message message)
    {
    if(message.getType() == Message.Type.chat)
    System.out.println(chat.getParticipant() + " says: " + message.getBody());
    }

    public static void main(String args[]) throws XMPPException, IOException
    {
    // declare variables
    JabberSmackAPI c = new JabberSmackAPI();
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    String msg;


    // turn on the enhanced debugger
    XMPPConnection.DEBUG_ENABLED = true;


    // Enter your login information here
    c.login("[B]username[/B]", "[B]password[/B]");

    c.displayBuddyList();

    System.out.println("-----");

    System.out.println("Who do you want to talk to? - Type contacts full email address:");
    String talkTo = br.readLine();

    System.out.println("-----");
    System.out.println("All messages will be sent to " + talkTo);
    System.out.println("Enter your message in the console:");
    System.out.println("-----\n");

    while( !(msg=br.readLine()).equals("bye"))
    {
        c.sendMessage(msg, talkTo);
    }

    c.disconnect();
    System.exit(0);
    }

}

 

The problem is that I get these errors among others:

The import org.jivesoftware.smack.Chat cannot be resolved
The import org.jivesoftware.smack.Roster cannot be resolved
The import org.jivesoftware.smack.RosterEntity cannot be resolved

Shouldn't these resolve after adding external jars to the project? This is the screenshot of all the external jars I have added to the project (thru Java Build Path):

1.JPG2.JPG

Am I missing something somewhere? Or is this example too old to run now? If yes then can I get a new example please? And also I want to mention that while running this example, I have setup nothing. I have not setup any server or anything. Do I need to setup something to be able to run this example above? Thanks really appreciate your help.

Outcomes