Basic start to use Smack

Hello, have a starting question. Follow example in smack documentation

$ cat myXMPPtest.java

public class myXMPPtest

extends Object

{

myXMPPtest con = new myXMPPtest(“jabber.server”);

con.connect();

con.login(“login”, “password”);

Chat chat = con.getChatManager().createChat(“user@jabber.server”, new MessageListener() {

public void processMessage(Chat chat, Message message) {

System.out.println("Received message: " + message);

}

});

chat.sendMessage(“Hello Test”);

con.disconnect();

}

so, compile it:

$ javac -classpath /var/local/lib/smack myXMPPtest.java

myXMPPtest.java:5: expected

con.connect();

^

/var/local/lib/smack contains all smack jars. Why smack jars not included in javac classpath?

How i can start to use smack?

p.s. $ java -version

java version “1.6.0_02”

Java™ SE Runtime Environment (build 1.6.0_02-b05)

Java HotSpot™ Client VM (build 1.6.0_02-b05, mixed mode, sharing)

Use

org.jivesoftware.smack.XMPPConnection

for your variable “con”.

The documentation I have says

// Create a connection to the jabber.org server.
XMPPConnection conn1 = new XMPPConnection("jabber.org");
conn1.connect();

You also don’t need the “extends Object”. You have to add “import” clauses on top of your code.

You can learn more about some basics in Suns very good Java tutorial:

http://java.sun.com/docs/books/tutorial

You can also get NetBeans (a Java IDE) for free, it can give you some hints about your coding:

http://www.netbeans.org/

Big thanks for answer. I read java doc about use classes, but still not work

import org.jivesoftware.smack.XMPPConnection; //before my public class XMPPConnection

XMPPConnection conn1 = new org.jivesoftware.smack.XMPPConnection(“jabber.org”);

or im stub?

hetzer wrote:

Big thanks for answer. I read java doc about use classes, but still not work

import org.jivesoftware.smack.XMPPConnection; //before my public class XMPPConnection

XMPPConnection conn1 = new org.jivesoftware.smack.XMPPConnection(“jabber.org”);

or im stub?

It looks Ok. EDIT: except, don’t call your class the same, “XMPPConnection”. Call it “MyJabberCon” or “MyJabberClient”, or whatever.

What is not working? Is it compiling? Surround all XMPP calls with a try-catch statement and have any exceptions printed out.

yes, it not compiling

dont understand, why javac not use smack’s jars.

$ ls /home/java

??? 1101

-rw-rr 1 hetzer users 471 2007-09-19 12:59 myJabber.java

-rw-rr 1 hetzer users 268021 2007-06-22 06:48 smack.jar

-rw-rr 1 hetzer users 54808 2007-06-22 06:48 smackx-debug.jar

-rw-rr 1 hetzer users 475428 2007-06-22 06:48 smackx.jar

-rw-rr 1 hetzer users 314460 2007-06-22 06:48 smackx-jingle.jar

$ cat /home/java/myJabber.java

import org.jivesoftware.smack.XMPPConnection;

public class myJabber

{

myJabber con = new org.jivesoftware.smack.XMPPConnection(“jabber.org”);

con.connect();

con.login(“user”, “password”);

Chat chat = con.getChatManager().createChat("user@jabber.org", new MessageListener() {

public void processMessage(Chat chat, Message message) {

System.out.println("Received message: " + message);

}

});

chat.sendMessage(“Hello Test”);

con.disconnect();

}

$ javac -classpath /home/java myJabber.java

myJabber.java:5: <identifier> expected

con.connect();

^

myJabber.java:6: <identifier> expected

con.login(“user”, “password”);

^

myJabber.java:6: illegal start of type

con.login(“user”, “password”);

^

myJabber.java:13: <identifier> expected

chat.sendMessage(“Hello Test”);

^

myJabber.java:13: illegal start of type

chat.sendMessage(“Hello Test”);

^

myJabber.java:14: <identifier> expected

con.disconnect();

^

6 errors

Use the code tags to format code you’re posting: blabla your code blabla.

hetzer wrote:

$ cat /home/java/myJabber.java

import org.jivesoftware.smack.XMPPConnection;

public class myJabber

{

myJabber con = new org.jivesoftware.smack.XMPPConnection(“jabber.org”);

Needs to read

XMPPConnection con = new org.jivesoftware.smack.XMPPConnection("jabber.org");

con.connect();

con.login(“user”, “password”);

Chat chat = con.getChatManager().createChat(“user@jabber.org”, new MessageListener() {

public void processMessage(Chat chat, Message message) {

System.out.println("Received message: " + message);

}

});

chat.sendMessage(“Hello Test”);

con.disconnect();

}

$ javac -classpath /home/java myJabber.java

myJabber.java:5: <identifier> expected

con.connect();

^

myJabber.java:6: <identifier> expected

con.login(“user”, “password”);

^

myJabber.java:6: illegal start of type

con.login(“user”, “password”);

^

myJabber.java:13: <identifier> expected

chat.sendMessage(“Hello Test”);

^

myJabber.java:13: illegal start of type

chat.sendMessage(“Hello Test”);

^

myJabber.java:14: <identifier> expected

con.disconnect();

^

6 errors

You made the variable “con” of type “myJabber” (also note that class names should start with a capital letter), instead of “XMPPConnection”. The compiler is telling you that the class “myJabber” does not have a method called “connect”, “login” etc. It is not telling you it can’t find the smack API.

You also need to make your class into an “EventListener”, by adding “implements PacketListener”. Otherwise the method “processPacket” won’t be called. Read this http://www.javaworld.com/javaworld/jw-09-1998/jw-09-techniques.html for how the event generator/listener stuff works.

Oh, my head got bomb effect…

So, i cant simple use another class like use lib or modules in another languages? I need to define each needed category if major class to my class? This is so strange or im really stub, dont understand this language…

Im give up. Can u show me part of working code in our examle?

hetzer wrote:

Oh, my head got bomb effect…

So, i cant simple use another class like use lib or modules in another languages? I need to define each needed category if major class to my class? This is so strange or im really stub, dont understand this language…

If it’s a Java library (.class or .jar), you use it through the “import” statement. And yes, you import each part of a package. It’s a little like the “include” in C.

Im give up. Can u show me part of working code in our examle?

You can download the source code for Spark. But it is really complicated for a beginner to Java. You might find more examples in these forums (go to the one called “smack”).

Work through the tutorial on Suns website (link was posted), so you can get a better understanding how Java works. Writing a XMPP client is probably not the best way of learning the language itself.

You’ll have to learn some basics first - the tutorial, and maybe some books (in your native language). Java has a steep learning curve, but once you understand the basics, you’ll be glad you did.

For Java questions, there are newsgroups (NNTP) like

comp.lang.java.help

comp.lang.java.programmer