powered by Jive Software

Ad-hoc command with smack

Hi all

I downloaded the classes for ad-hoc commands but I am failing using it.

Below is the code, server name and login omitted. The error follows

public static void main(String[] args) {

try{

String JABBER_SERVER = “”;

String LOGIN = “”;

String PASSWD = “”;

String buddyJID = “@“JABBER_SERVER”/Home”;

//open connection and create accounts

ConnectionConfiguration conf = new ConnectionConfiguration(JABBER_SERVER, 5222);

XMPPConnection connection = new XMPPConnection(conf);

AdHocCommandManager commandManager = AdHocCommandManager.getAddHocCommandsManager(connection);

connection.connect();

AccountManager accountManager = connection.getAccountManager();

if(accountManager.supportsAccountCreation()){

try{

accountManager.createAccount(LOGIN, PASSWD);

}

catch(XMPPException ex){

//account created already

}

}

connection.login(LOGIN, PASSWD);

Roster roster = connection.getRoster();

if(roster.getEntry(buddyJID)==null){

String[] groups = {“Home”};

roster.createEntry(buddyJID, “OEG”, groups);

}

//start using commands

//at this stage commandManager==null for me WHY???

if(commandManager==null)

commandManager = AdHocCommandManager.getAddHocCommandsManager(connection);

DiscoverItems cmds = commandManager.discoverCommands(buddyJID);

for(Iterator it2 = cmds.getItems(); it2.hasNext(); ){

DiscoverItems.Item item = (DiscoverItems.Item)it2.next();

System.out.println(“item”+item.toXML());

}

RemoteCommand cmd = commandManager.getRemoteCommand(buddyJID, “GetScreenSensors”);

System.out.println(“cmd"cmd.getName()”|"+cmd.getNode());

cmd.execute();

Form answer = cmd.getForm();

System.out.println(“answer"answer.getTitle()”|"+answer.getType());

for(Iterator<FormField> it = answer.getFields(); it.hasNext(); ){

FormField field = it.next();

System.out.println(“field”+field.toXML());

}

connection.disconnect();

}

catch(Exception ex){

ex.printStackTrace();

}

}

Here is the error

item<item jid=“omitted@omitted” name=“Get the screen sensors” node=“GetScreenSensors”/>

cmdnull|GetScreenSensors

java.lang.ClassCastException: org.jivesoftware.smack.PacketReader$4

at org.jivesoftware.smackx.commands.RemoteCommand.executeAction(RemoteCommand.java :153)

at org.jivesoftware.smackx.commands.RemoteCommand.executeAction(RemoteCommand.java :110)

at org.jivesoftware.smackx.commands.RemoteCommand.execute(RemoteCommand.java:83)

at twm.msg.InstantMsger3.main(InstantMsger3.java:61)

The line 153 in RemoteCommand is:

AdHocCommandData responseData = (AdHocCommandData) response;

I am out of idea and you?

Thanks

Matthieu

Hi all,

Well the answer was simple.

Do not forget to rebuild the jars including the ad-hoc command configuration files from META-INF directory !!!

Matthieu