How to compile?

I originally asked for some help on another forum. It appears that none of the existing perl or python XMPP tools work with Openfire. I need to be able to connect to openfire via command line and send messages to people logged in–certain events in UNIX require immediate attention.

The only suggestion I got was to learn to use the Smack API. The documents may be complete for an experienced programmer, but certainly not for someone who has no exposure to Java. There does not appear to be any information on how to compile the (slightly modified) example code:

XMPPConnection connection = new XMPPConnection(“jabber.sav.local”);

connection.login(“sdaemon”, “732bJd24”);

Message message = new Message();

message.setTo(“kenk@jabber.sav.local”);

message.setSubject(“Server down”);

message.setBody(“server alert test”);

message.setType(Message.Type.HEADLINE);

connection.sendPacket(message);

connection.close();

This code appears to do exactly what I want, but the command “javac -classpath . test.java” generates errors on every line. The jar files are in the same directory as the javac, and other demo java sources compile fine. It is not reasonable for me to become java-competent in one night or even in many days. What do I have to do to compile the java supplied in the documentation?

I will eventually compile this for linux, but right now I just need to prove to myself this is doable. I can’'t seem to get past the first step.

-John

Okay, you’'ll need to do a few things to get it working but its shouldnt be that hard.

What I’'d do is the following.

1: Download smack api

2: Unpack the folder somewhere, say C:\smack

3: Write your code. You will need to add an import statement at the top like so

import org.jivesoftware.smack.*;
import org.jivesoftware.smackx.*; public class ConnectionModel {}

4: Save it somewhere, say MyDocuments\Java\InstantMessaging\ConnectionModel.java

5: Compile your code like so javac -cp C:\smack\smack.jar;C:\smack\smackx.jar;"." ConnectionModel.java

6: If all went okay you should be able to run it using java -cp C:\smack\smack.jar;C:\smack\smackx.jar;"." ConnectionModel providing ConnectionModel has a main method in it

hth

Jon

Ok, first, thank you for the help. I have followed your instructions. As nearly as I can tell, the code appears to be correct. The javac command with parameters also appears to be correct. I have verified that the jars are in the smack directory. I have saved the java file in the same c:\smack directory for simplicity.

— begin java code —

import org.jivesoftware.smack.*;

import org.jivesoftware.smackx.*;

public class ConnectionModel {

public static void main(String [] args)

{

XMPPConnection connection = new XMPPConnection(“jabber.sav.local”);

connection.login(“sdaemon”, “732bJd24”);

Message message = new Message();

message.setTo(“kenk@jabber.sav.local”);

message.setSubject(“Server down”);

message.setBody(“server alert test”);

message.setType(Message.Type.HEADLINE);

connection.sendPacket(message);

connection.close();

}

}

— end java code —

Here’'s the output of my compile attempt, can you give me an idea on what is wrong here?

C:\smack>“c:\Program Files\Java\jdk1.6.0_01\bin\javac.exe” -cp C:\smack\smack.jar;c:\smack\smackx.jar;"." ConnectionModel.java

ConnectionModel.java:11: cannot find symbol

symbol : class Message

location: class ConnectionModel

Message message = new Message();

^

ConnectionModel.java:11: cannot find symbol

symbol : class Message

location: class ConnectionModel

Message message = new Message();

^

ConnectionModel.java:15: package Message does not exist

message.setType(Message.Type.HEADLINE);

^

ConnectionModel.java:17: cannot find symbol

symbol : method close()

location: class org.jivesoftware.smack.XMPPConnection

connection.close();

^

4 errors

Hi John,

I was able successfully to compile your code snippet, but using some adjustments, see the code attached bellow.


Java code begin


import org.jivesoftware.smack.packet.Message;

import org.jivesoftware.smack.packet.*;

import org.jivesoftware.smack.*;

import org.jivesoftware.smackx.*;

public class ConnectionModel {

public ConnectionModel() {

Initialize();

}

public void Initialize() {

XMPPConnection connection = new XMPPConnection(“jabber.sav.local”);

try

{

connection.connect();

connection.login(“sdaemon”, “732bJd24”);

Message message = new Message();

message.setTo(“kenk@jabber.sav.local”);

message.setSubject(“Server down”);

message.setBody(“server alert test”);

message.setType(Message.Type.headline);

connection.sendPacket(message);

connection.disconnect();

}

catch (XMPPException e) {

e.printStackTrace();

}

}

public static void main(String [] args) {

ConnectionModel connmodel = new ConnectionModel();

}

}


Java code end


For some reason importing the whole smack package ( import org.jivesoftware.smack; ) does not include the Message class which is declared and implemented in the packet sub-package of smack.

As per declaration of the enumeration Type enclosed in the class Message, the enum members have lowcase letters so instead of writing Message.Type.HEADLINE you should write Message.Type.headline.

Furthermore the class XMPPConnection does not have the method close(), but includes two overloads of the method disconnect and one method shutdown.

Hope to helped you.

Kind regards,

sbogus.