Oct 28

Writing a gTalk (Jabber/XMPP) client in Java

Tag: Java, ProgrammingAbhijeet Maharana @ 12:17 am

Screenshot

From its Wikipedia entry, “Extensible Messaging and Presence Protocol (XMPP) is an open, XML-inspired protocol for near-real-time, extensible instant messaging (IM) and presence information (a.k.a. buddy lists). It is the core protocol of the Jabber Instant Messaging and Presence technology. The protocol is built to be extensible and other features such as Voice over IP and file transfer signaling have been added.”

There are a large number of XMPP server and client implementations and code libraries. We will use the Smack open source Java library to implement a simple client that can connect to any Jabber service. Google’s messaging service uses the same protocol and we will use it for the example.

Create a folder for your project. Create a “lib” subfolder.
Download the Smack library and extract the following files to the lib folder:

smack.jar

  • smackx.jar (for the extensions)
  • smackx-debug.jar (for the enhanced debugger)
    Debugger screenshots: 1 | 2 | 3 | 4

    As of this writing, the Smack API is in version 3.0.4 (build date: June 12, 2006). Create ChatClient.java and use the code snippets below for the methods of this class. You can also download the project files from the link at the end of this post.

    The code below shows how to connect to the gTalk service:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    
    // import org.jivesoftware.smack.ConnectionConfiguration;
    // import org.jivesoftware.smack.XMPPConnection;
     
    ConnectionConfiguration config = 
    	new ConnectionConfiguration("talk.google.com", 
    					5222, 
    					"gmail.com");
    connection = new XMPPConnection(config);
    connection.connect();
    connection.login(your_username, your_password);


    “talk.google.com” is the host. 5222 is the port number and “gmail.com” is the service name. Although they are different here but usually, the service name is same as the hostname. Let us now get hold of the buddy list:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    
    // import org.jivesoftware.smack.Roster;
    // import org.jivesoftware.smack.RosterEntry;
     
    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());
    }


    We can also send a message to a buddy by using the Chat class:

    1
    2
    3
    4
    
    //import org.jivesoftware.smack.Chat;
     
    Chat chat = connection.getChatManager().createChat(to, this);
    chat.sendMessage(message);

    The first argument is quite obvious. The second argument is an instance of a class that implements the org.jivesoftware.smack.MessageListener interface. This listener receives incoming chat messages and calls the interface method

    1
    
     public void processMessage(Chat chat, Message message)

    to process the message. We can now disconnect from the messenger service:

    1
    
    connection.disconnect();

    Thus, we have a bare-bones Jabber client. The complete program looks like this. You can download the project (see update below) with instructions on compiling and running it. It has a hard-coded username and password which will have to be changed. You will also need to enter the email address of your gTalk buddy before sending any message. The person you are trying to communicate with should already be in your buddy list.

    Update (10 July 2009):
    Check this out for SASL authentication errors: http://server.everfine.com.tw/blog/archives/2009/06/smack-api.html

    Update (30 December 2009):
    I no longer have a copy of the original project archive that I posted.
    However, a modified version is available here: http://groups.google.com/group/jujuyjug/files?pli=1

  • 98 Responses to “Writing a gTalk (Jabber/XMPP) client in Java”

    1. chaitanya says:

      Hi Abhijeet

      I got the following error while running this program.

      In fact, I am getting this error from some other implementation also. Where could it went wrong?

      Exception in thread “main” Could not connect to talk.google.com:5222.: remote-server-timeout(504) Could not connect to talk.google.com:5222.
      — caused by: java.net.UnknownHostException: talk.google.com
      at org.jivesoftware.smack.XMPPConnection.connectUsingConfiguration(XMPPConnection.java:823)
      at org.jivesoftware.smack.XMPPConnection.connect(XMPPConnection.java:1276)
      at ChatClient1.login(ChatClient1.java:25)
      at ChatClient1.main(ChatClient1.java:71)
      Nested Exception:
      java.net.UnknownHostException: talk.google.com
      at java.net.PlainSocketImpl.connect(Unknown Source)
      at java.net.SocksSocketImpl.connect(Unknown Source)
      at java.net.Socket.connect(Unknown Source)
      at java.net.Socket.connect(Unknown Source)
      at java.net.Socket.(Unknown Source)
      at java.net.Socket.(Unknown Source)
      at org.jivesoftware.smack.XMPPConnection.connectUsingConfiguration(XMPPConnection.java:815)
      at org.jivesoftware.smack.XMPPConnection.connect(XMPPConnection.java:1276)
      at ChatClient1.login(ChatClient1.java:25)
      at ChatClient1.main(ChatClient1.java:71)

    2. Abhijeet Maharana says:

      Hi Chaitanya,
      Even though it doesn’t seem to be a problem with the program, I ran it again and was able to chat just fine.
      Problem seems to be with your internet connectivity. Are you able to ping talk.google.com?

    3. Abhijeet Maharana says:

      Chaitanya (via mail):
      =============

      Hi Abhi

      I am not able to resolve it.

      I am not able to connect to talk.google.com through my program.

      One reason could be I m trying from office as I don’t have net connection as of now at home.

      Still have to try.

      If you know the reason please let me know.

      Thank you,
      Chaitanya

    4. Abhijeet Maharana says:

      Can you ping talk.google.com from the command line?
      You are trying from office … then it could also be a proxy server issue.

      If you are using IE, go to Tools >> Internet Options >> Connections >> LAN Settings. See if there is a proxy server specified there. If not, there might be an automatic configuration script specified. Take a look at that script and see if you can figure out the proxy server being used.

      Also, check that talk.google.com is not blocked from your office. Some companies have their internal messaging solutions and block others.

    5. chaitanya says:

      Hi Abhi

      talk.google.com is accessible. It is not blocked yet :-) ) from my office.( Infosys- bangalore)

      It is some proxy server issue. I will explore and let u know.

      Unfortunately, getting Net at my home is getting delayed to try these kinda things.

      Thank you,
      Chaitanya

    6. SMACK API - need help w sample ChatClient - BlackBerryForums.com : Your Number One BlackBerry Community says:

      [...] if you guys have all the necessary info itll go smoother.. Trying to load the sample from … Abhijeet Maharana » Writing a gTalk (Jabber/XMPP) client in Java getting the following errors… generics are not supported in -source 1.3 (try -source 1.5 to [...]

    7. James Murphy says:

      Hello, im trying to test this on a BlackBerry handheld. Have you any experience with J2ME or the
      BlackBerry JDE 4.3.0(BB JDE) ? I have posted the 2 errors im recieving from the BB JDE to the
      BlackBerry Forums.. http://www.blackberryforums.com/developer-forum/127332-smack-api-need-help-w-sample-chatclient.html

    8. Abhijeet Maharana says:

      Hi James,
      My *pure guess* below since I have no experience with Blackberry.

      Options:
      1. Try passing “-source 1.5″ parameter. You will need to look up the documentation for rapc.exe to figure out how to do that.

      2. Don’t use generics and for-each loop.

      public void displayBuddyList()
      {
      	Roster roster = connection.getRoster();
      	Collection entries = roster.getEntries();
       
      	System.out.println("\n\n" + entries.size() + " buddy(ies):");
      	Iterator i = entries.iterator();
       
      	while(i.hasNext())
      	{
      		RosterEntry r = (RosterEntry) i.next();
      		System.out.println(r.getUser());
      	}
      }

      Above code is not tested. You may need to fix minor issues.

      Do let me know if this fixes your issue.

    9. James Murphy says:

      Thanks, Ill check that out today.

      What are generics? and how do i addin the smack api libraries? I understand you dont have any experience
      with the BlackBerry JDE but, how would one addin the smack api using another IDE? I tried copying the .jar’s
      to the /bin folder to no avail. Thanks for your assistance.

      If you curious, I can assist you in loading the BlackBerry JDE and also a BlackBerry simulator handheld and
      you would be on your way to developing apps for blackberry. I have many many samples.. then, hopefully
      you could take me under your wing. I could use a mentor. :)

    10. Abhijeet Maharana says:

      Generics enable compile-time type checking for collections making them safer to use. http://java.sun.com/j2se/1.5.0/docs/guide/language/generics.html

      Most IDEs have something under project properties to add jars to the class path. Eclipse has a “build path” feature and once you add your jar here, it is passed to the compiler and runtime when the project is launched.

      Many thanks for the generous offer. I could definitely use some help if and when I start exploring blackberry development. I think I should start using a blackberry now! And I would rather have it the other way round: you as my mentor! If you use twitter, I am available at http://twitter.com/maharana

      Regards.

    11. James Murphy says:

      Consider this.. I have nearly zero experience with Java. I can get you up to speed with the BlackBerry
      JDE and then ill have to hand over the mentorship. ;)

    12. James Murphy says:

      I will give you the links for getting started with BlackBerry Dev using the BlackBerry JDE 4.3.0

      Download BlackBerry JDE 4.3.0 from http://na.blackberry.com/eng/developers/downloads/jde.jsp

      Download simulators and the BlackBerry Email and MDS Services Simulator Package 4.1.4,
      I use the 4.2.2 versions of the simulator. I prefer the 8300 series BlackBerry handhelds,
      http://na.blackberry.com/eng/developers/downloads/simulators.jsp

      once all this is loaded then make sure you have C:\Program Files\Research In Motion\BlackBerry JDE 4.3.0\bin
      in your path. Once you are this far we can continue with the training. :)

    13. James Murphy says:

      BTW I found an option to import .JAR files into a project. That must be it..

    14. Abhijeet Maharana says:

      I did try to download the Eclipse plugin. 200+ MB with a weird download method. I couldn’t use a download manager and it broke mid way :( Will try again tonight. Or go with the smaller JDE download. Good that you found that option. Let me know if you are able to get the chat client working.

    15. James Murphy says:

      I just use the BB JDE. I made the changes you suggested and im still getting exceptions, in strange places..

      Like ‘missing symbol’ on collections.. ect. ill post some output later..

    16. James Murphy says:

      here is the code..

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      29
      30
      31
      32
      33
      34
      35
      36
      37
      38
      39
      40
      41
      42
      43
      44
      45
      46
      47
      48
      49
      50
      51
      52
      53
      54
      55
      56
      57
      58
      59
      60
      61
      62
      63
      64
      65
      66
      67
      68
      69
      70
      71
      72
      73
      74
      75
      76
      77
      78
      79
      80
      81
      82
      83
      84
      85
      86
      87
      
      import java.util.*;
      import java.io.*;
       
      import org.jivesoftware.smack.Chat;
      import org.jivesoftware.smack.packet.Message;
      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;
       
      public class ChatClient implements MessageListener
      {
           XMPPConnection connection;
       
             public void login(String userName, String password) throws XMPPException
             {
          	   ConnectionConfiguration config = new ConnectionConfiguration("talk.google.com", 5222, "gmail.com");
                 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 entries = roster.getEntries();
       
          	   System.out.println("\n\n" + entries.size() + " buddy(ies):");
          	   Iterator i = entries.iterator();
       
          	   while(i.hasNext())
          	   {
          		   RosterEntry r = (RosterEntry) i.next();
          		   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
                 ChatClient c = new ChatClient();
                 BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
                 String msg;
       
                 // turn on the enhanced debugger
                 XMPPConnection.DEBUG_ENABLED = true;
       
                 // provide your login information here
                 c.login("madnesstestuser", "password");
       
       
                 c.displayBuddyList();
                 System.out.println("-----");
                 System.out.println("Enter your message in the console.");
                 System.out.println("All messages will be sent to abhijeet.maharana");
                 System.out.println("-----\n");
       
                 while( !(msg=br.readLine()).equals("bye"))
                 {
              	   // your buddy's gmail address goes here
                     c.sendMessage(msg, "abhijeet.maharana@gmail.com");
                 }
       
                 c.disconnect();
                 System.exit(0);
             }
      }
    17. James Murphy says:

      here is the output..

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      29
      30
      31
      32
      33
      34
      
      Building ChatClient
      C:\Program Files\Research In Motion\BlackBerry JDE 4.3.0\bin\rapc.exe  -quiet import="..\..\..\..\..\Program Files\Research In Motion\BlackBerry JDE 4.3.0\lib\net_rim_api.jar";ChatClient\lib\smack.jar codename=ChatClient\ChatClient ChatClient\ChatClient.rapc warnkey=0x52424200;0x52435200;0x52525400 "C:\Documents and Settings\jmu\Desktop\jbmJDE\JBM\ChatClient\CC\ChatClient.java"
      C:\Documents and Settings\jmu\Desktop\jbmJDE\JBM\ChatClient\CC\ChatClient.java:37: cannot find symbol
      symbol  : class Collection
      location: class ChatClient
              Collection entries = roster.getEntries();
              ^
      C:\Documents and Settings\jmu\Desktop\jbmJDE\JBM\ChatClient\CC\ChatClient.java:40: cannot find symbol
      symbol  : class Iterator
      location: class ChatClient
        Iterator i = entries.iterator();
        ^
      C:\Documents and Settings\jmu\Desktop\jbmJDE\JBM\ChatClient\CC\ChatClient.java:56: cannot access java.lang.Enum
      file java\lang\Enum.class not found
                    if(message.getType() == Message.Type.chat)
                                         ^
      C:\Documents and Settings\jmu\Desktop\jbmJDE\JBM\ChatClient\CC\ChatClient.java:64: cannot find symbol
      symbol  : class BufferedReader
      location: class ChatClient
                     BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
                     ^
      C:\Documents and Settings\jmu\Desktop\jbmJDE\JBM\ChatClient\CC\ChatClient.java:64: cannot find symbol
      symbol  : class BufferedReader
      location: class ChatClient
                     BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
                                             ^
      C:\Documents and Settings\jmu\Desktop\jbmJDE\JBM\ChatClient\CC\ChatClient.java:64: cannot find symbol
      symbol  : variable in
      location: class java.lang.System
                     BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
                                                                                        ^
      6 errors
      Error!: Error: java compiler failed: javac -source 1.3 -target 1.1 -g -O -d C:\DOCUME~1\jmu\LOCALS~1\Temp\rapc_555d6fe7.dir -bootcla ...
      Error while building project
    18. Abhijeet Maharana says:

      Can’t really be of any help here. Most likely, the runtime is different with a different API. But, I am merely *guessing* here. The blackberry forum will be a better place as you will get some definitive answers there.

      Regards,
      Abhijeet.

    19. kpbird says:

      Can I Transfer file using this library ?

    20. Abhijeet Maharana says:

      Hi,
      I did a quick search of the igniterealtime forums and came across this: http://www.igniterealtime.org/community/message/118501#118501

    21. Adriel Blog - Let’s share our passion » XMPP services says:

      [...] There are also Java based libraries that can be used for the custom Java client development such as http://abhijeetmaharana.com/blog/2007/10/28/writing-a-gtalk-jabberxmpp-client/ [...]

    22. Daniel says:

      I read similar article also named a gTalk (Jabber/XMPP) client in Java | Abhijeet Maharana, and it was completely different. Personally, I agree with you more, because this article makes a little bit more sense for me

    23. Siddharth says:

      I am behind a proxy server. How can I use smack API to connect to gtalk ?

    24. Abhijeet Maharana says:

      This should help: http://beerholder.blogspot.com/2008/07/tunneling-eclipse-communication.html
      I haven’t tried doing it though. Don’t have a Ubuntu installation nearby. Do let me know how it works out.

    25. sweety says:
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      29
      30
      31
      32
      33
      34
      35
      36
      37
      38
      39
      40
      41
      42
      43
      44
      45
      46
      47
      48
      49
      50
      51
      52
      53
      54
      55
      56
      57
      58
      59
      60
      61
      62
      63
      64
      65
      66
      67
      68
      69
      70
      71
      72
      73
      74
      75
      76
      77
      78
      79
      80
      81
      82
      83
      84
      85
      86
      87
      88
      89
      90
      91
      92
      93
      94
      95
      96
      97
      98
      99
      100
      101
      102
      103
      104
      105
      106
      107
      108
      
      I want to create chat software in java .I read above information very carefully 
      I am using apache tomcat ,jdk1.5 and I download project from above link put it in webapps folder and set path as 
      export CLASS_PATH=/usr/local/LinuxApt/apache-tomcat-5.5.12/webapps/gTalkClient/lib/smack.jar:/usr/local/LinuxApt/apache-tomcat-5.5.12/webapps/gTalkClient/lib/smackx.jar:/usr/local/LinuxApt/apache-tomcat-5.5.12/webapps/gTalkClient/lib/smackx-debug.jar
      it shows me a lot errors
      then i set path as
      export CLASS_PATH=/usr/local/LinuxApt/apache-tomcat-5.5.12/webapps/gTalkClient/lib/org:/usr/local/LinuxApt/apache-tomcat-5.5.12/webapps/gTalkClient/lib/com;
      still same errors like
       
      ChatClient.java:5: package org.jivesoftware.smack does not exist import org.jivesoftware.smack.Chat;
                                    ^
      ChatClient.java:6: package org.jivesoftware.smack.packet does not exist
      import org.jivesoftware.smack.packet.Message;
                                           ^
      ChatClient.java:7: package org.jivesoftware.smack does not exist
      import org.jivesoftware.smack.ConnectionConfiguration;
                                    ^
      ChatClient.java:8: package org.jivesoftware.smack does not exist
      import org.jivesoftware.smack.MessageListener;
                                    ^
      ChatClient.java:9: package org.jivesoftware.smack does not exist
      import org.jivesoftware.smack.Roster;
                                    ^
      ChatClient.java:10: package org.jivesoftware.smack does not exist
      import org.jivesoftware.smack.RosterEntry;
                                    ^
      ChatClient.java:11: package org.jivesoftware.smack does not exist
      import org.jivesoftware.smack.XMPPConnection;
                                    ^
      ChatClient.java:12: package org.jivesoftware.smack does not exist
      import org.jivesoftware.smack.XMPPException;
                                    ^
      ChatClient.java:15: cannot find symbol
      symbol: class MessageListener
      public class ChatClient implements MessageListener
                                         ^
      ChatClient.java:17: cannot find symbol
      symbol  : class XMPPConnection
      location: class ChatClient
              XMPPConnection connection;
              ^
      ChatClient.java:19: cannot find symbol
      symbol  : class XMPPException
      location: class ChatClient
              public void login(String userName, String password) throws XMPPException                                                                   ^
      ChatClient.java:28: cannot find symbol
      symbol  : class XMPPException
      location: class ChatClient
              public void sendMessage(String message, String to) throws XMPPException
                                                                        ^
      ChatClient.java:51: cannot find symbol
      symbol  : class Chat
      location: class ChatClient
              public void processMessage(Chat chat, Message message)
                                         ^
      ChatClient.java:51: cannot find symbol
      symbol  : class Message
      location: class ChatClient
              public void processMessage(Chat chat, Message message)
                                                    ^
      ChatClient.java:57: cannot find symbol
      symbol  : class XMPPException
      location: class ChatClient
              public static void main(String args[]) throws XMPPException, IOException                                                      ^
      ChatClient.java:21: cannot find symbol
      symbol  : class ConnectionConfiguration
      location: class ChatClient
                      ConnectionConfiguration config = new ConnectionConfiguration("talk.google.com", 5222, "gmail.com");
                      ^
      ChatClient.java:21: cannot find symbol
      symbol  : class ConnectionConfiguration
      location: class ChatClient
                      ConnectionConfiguration config = new ConnectionConfiguration("talk.google.com", 5222, "gmail.com");
                                                           ^
      ChatClient.java:22: cannot find symbol
      symbol  : class XMPPConnection
      location: class ChatClient
                      connection = new XMPPConnection(config);
                                       ^
      ChatClient.java:30: cannot find symbol
      symbol  : class Chat
      location: class ChatClient
                      Chat chat = connection.getChatManager().createChat(to, this);
                      ^
      ChatClient.java:36: cannot find symbol
      symbol  : class Roster
      location: class ChatClient
                      Roster roster = connection.getRoster();
                      ^
      ChatClient.java:37: cannot find symbol
      symbol  : class RosterEntry
      location: class ChatClient
                      Collection entries = roster.getEntries();
                                 ^
      ChatClient.java:40: cannot find symbol
      symbol  : class RosterEntry
      location: class ChatClient
                      for(RosterEntry r:entries)
                          ^
      ChatClient.java:53: package Message does not exist
                      if(message.getType() == Message.Type.chat)
                                                     ^
      ChatClient.java:66: cannot find symbol
      symbol  : variable XMPPConnection
      location: class ChatClient
                      XMPPConnection.DEBUG_ENABLED = true;
                      ^
      24 errors
      [root@localhost gTalkClient]#

      is there is mistakes in my settings
      please guide me properly
      eagerly waitng for reply

    26. sweety says:

      one more it gives me error if i set class path as you mention in readme.txt in gtalkClient project

    27. sweety says:

      where should i enter my buddies name?

    28. Abhijeet Maharana says:

      Hi, thanks for dropping by!

      This is not a web application. So you don’t need Tomcat. Just compile and run it from the command line. The readme file has commands for windows. On Unix, replace ; with : and \ with /.

      $ javac -cp lib/smack.jar:lib/smackx.jar:lib/smackx-debug.jar ChatClient.java
      $ java -cp lib/smack.jar:lib/smackx.jar:lib/smackx-debug.jar:. ChatClient

      I have put comments where you need to provide your login information and your buddy’s gmail id. Look at the main method of ChatClient.java.
      Hope this helps.

      PS: This is just a command line sample to get you started. There is no fancy UI.

    29. sweety says:

      very good maorning
      really thanks Abhijeet for response
      this is really helpful for me

      please tell me about setting of classpath
      in linux classpath is set in /root/.bash_profile and .bashrc
      tell me as early as possible
      thanks.

    30. sweety says:

      i set path as in /root
      .bash_profile
      export CLASS_PATH=/usr/local/gtalkClient/lib/smack.jar:/usr/local/gtalkClient/lib/smackx.jar:/usr/local/gtalkClient/lib/smackx-debug.jar

      heyyyyyyyyy
      and its working properly
      one window is open showing status.
      but i am not getting meaning of IQ and how can i send message. please send me

      thanks

    31. sweety says:

      my out put is like
      23 buddy(ies):
      EDIT (abhijeet): I have removed your contacts’ addresses from here
      —–
      Enter your message in the console.
      All messages will be sent to abhijeet.maharana
      —–

      Exception in thread “Smack Packet Reader (0)” java.lang.ExceptionInInitializerEr ror
      at org.jivesoftware.smackx.provider.DelayInformationProvider.parseExtens ion(DelayInformationProvider.java:51)
      at org.jivesoftware.smack.util.PacketParserUtils.parsePacketExtension(Pa cketParserUtils.java:378)
      at org.jivesoftware.smack.util.PacketParserUtils.parseMessage(PacketPars erUtils.java:107)
      at org.jivesoftware.smack.PacketReader.parsePackets(PacketReader.java:27 2)
      at org.jivesoftware.smack.PacketReader.access$000(PacketReader.java:44)
      at org.jivesoftware.smack.PacketReader$1.run(PacketReader.java:76)
      Caused by: java.lang.NullPointerException
      at java.util.TimeZone.parseCustomTimeZone(TimeZone.java:780)
      at java.util.TimeZone.getTimeZone(TimeZone.java:484)
      at java.util.TimeZone.getTimeZone(TimeZone.java:478)
      at org.jivesoftware.smackx.packet.DelayInformation.(DelayInforma tion.java:51)
      … 6 more

    32. sweety says:

      what about chat window??????????????

    33. sweetsweety says:

      please reply

    34. Abhijeet Maharana says:

      1. As I have already mentioned in the post and in the comment above, this is a command line *only* sample. The window that opens is for debugging. You can’t enter chat messages there. Chat messages have to be entered in the console where you run the program.

      2. See the screenshot at http://abhijeetmaharana.com/blog/bloguploads/gtalkclient/gtalkclient.JPG. A test user is chatting with abhijeet.maharana from the command prompt on the left.

      3. I am using Windows and I am not getting the exception you have reported above. Mail me your program. Remove your password but keep everything else intact. Ill try to run it and see if I can reproduce the above exception.

    35. Manoj says:

      Hi Abhi,

      Is it Possible to Capture GTalk Conservation and Synchronizing with Outlook. Pl let me Know

    36. Manoj says:

      Hi Abhi,

      Is Jabber can be Customised to use GTalk and Synchronizing Chat Conservations to Oulook

    37. Abhijeet Maharana says:

      Hi Manoj, thanx for dropping by.
      For the first part, this should help: http://blogoscoped.com/archive/2006-05-02-n79.html
      I didn’t get what you mean by “Synchronizing Chat Conservations to Oulook” ???

    38. sweetsweety says:

      Hi abhi
      once again i start work on gtalk ………….
      i am successful to run it on linux redhat but not able to succeful to display all chtting on terminal(command promt)(debug shows me availability of my friend and all chat messages).sometimes some chats not display on prompt .If you have any idea please guide me.

    39. Abhijeet Maharana says:

      Hello Sweety, I am afraid I won’t be able to spend time on the issue you have mentioned. Apologies. However, I would strongly recommend that you join the IgniteRealtime forums. I am sure you will get a few answers there. http://www.igniterealtime.org/community/index.jspa

      Regards,
      Abhijeet.

    40. Deepak says:

      Hi Abhijeet,

      I am trying to use connect the GTalk using the method and the process as mentioned by you above but I am getting the following Error

      09-02 22:09:38.984: INFO/XMPP Error(3926): Could not connect to talk.google.com:5222.: remote-server-timeout(504) Could not connect to talk.google.com:5222.

    41. Abhijeet Maharana says:

      Are you able to ping the server directly? Or maybe you are behind a proxy? http://beerholder.blogspot.com/2008/07/tunneling-eclipse-communication.html

    42. Deepak says:

      Hi Abhijeet,

      i am able to sucessfully run it as described you but when I tried to run on the Android platform by importing the smack jar it is giving me this error

      09-05 14:24:54.964: WARN/System.err(2403): Could not connect to talk.google.com:5222.: remote-server-timeout(504) Could not connect to talk.google.com:5222.
      09-05 14:24:54.974: WARN/System.err(2403): — caused by: java.net.UnknownHostException: talk.google.com – talk.google.com
      09-05 14:24:54.994: WARN/System.err(2403): at org.jivesoftware.smack.XMPPConnection.connectUsingConfiguration(XMPPConnection.java:823)
      09-05 14:24:54.994: WARN/System.err(2403): at org.jivesoftware.smack.XMPPConnection.connect(XMPPConnection.java:1276)

      Does I have modify the smack jar? If yes please tell me how.

    43. Abhijeet Maharana says:

      Deepak, I do not have any experience with Android yet. Can’t be of any help here!

      EDIT: This should help.
      From a quick Google search: http://davanum.wordpress.com/2007/12/31/android-just-use-smack-api-for-xmpp/

    44. Anthony says:

      I am trying to figure out how you in the screenshot you got it to stream incoming messages to the commandline. As near as I can tell is your sample you never actually call processMessage() which dumps incoming messages to the screen as your jpg shows. Can you clarify?

    45. Abhijeet Maharana says:

      Hi Anthony, thanks for dropping by!
      processMessage() belongs to the MessageListener interface. An instance of a class implementing this interface is registered with the Chat instance created for a buddy using createChat(). It is then the Chat instance’s responsibility to invoke processMessage() when a message is received from the buddy. See this.

      In the example above, sendMessage() does this for us. 2nd parameter is ‘this’ because our main class itself implements MessageListener.

      Chat chat = connection.getChatManager().createChat(to, this);

      Actually, since we are sending all messages to the same buddy, we can move this line outside sendMessage(). Hope this answers your question.

    46. MethoD says:

      My school closed most of the ports including 5222.

      Can I use smack API to chat with gmail users?

    47. Abhijeet Maharana says:

      meebo.com? In case its blocked as well, you can use proxies [which you know are good] to access it.
      As for your other question, I’m not sure at the moment. Will have to read up and try out things to be able to answer it.

    48. a. shiraz says:

      is there a c/ c++ version of this tutorial please?

      any c/ c++ libraries that are similar to smack?

    49. Abhijeet Maharana says:

      Hi,
      This is the only version of this tutorial at the moment.
      For a C version, you can try the Loudmouth project http://www.loudmouth-project.org/about.

    50. Mari says:

      Hi Abhijeet,

      Thanks for providing the code in your blog. it gives the good understanding of basics.

      I am facing a strange issue !!

      On running the code in my machine, I am able to chat with my userID as both sender and receiver (in console, as just echo program). However, while chatting with another gmailID, My msgs are reaching the reveiver to thier gtalk, but thier replys are not displayed in my console.

      I can see the reply packets are received in the degugger window, but processMessage() of MessageListener is not getting invoked in this case.

      The same problem occurs, even with my local Openfire installed in my machine.

      Please provide me some insight on this issue.

      Thanks,
      Mari

    51. Abhijeet Maharana says:

      Hi Mari,
      Did you miss out the registration of MessageListener?

    52. Sandun Lewke Bandara says:

      Your appication works really well.i used Netbeans IDE and it worked perfectly.Do you have gtalk voice capable java application buddy? may i know the link to download the source code of that programme…

    53. Sandun says:

      your application worked really fine. i used it with Netbeans IDE and it was really cool. Do you have gtalk voice capable smack sample like this one?

    54. Sandun Lewke Bandara says:

      Hi im again…
      Seems your given project need some changes to work with Smack 3.1.0.
      DO you have that updated project? Please give me a help as im a newbie to Programming.
      i tested many times your application and found Messages sends to other client perfectly. but when i receive some msgs from the client, it some times failed to display the received msg at the command line.any explanation about this case??

    55. Abhijeet Maharana says:

      Hi Sadun, I havent been able to work on this any further. Havent tested with 3.1.0. So can’t be sure what the issue is. Do let me know if you manage to isolate the issue. Looks like Mari’s issue was related.

    56. Sandun says:

      Thanks for replying.. anyway how do i use google talk voice functionality using smack and Java SE platform?? i wanna use gtalk’s voice for my project on java.pls help me if you know those stuffs.if you have a project which used google talk voice pls send it to me on sandun1234u@gmail.com

    57. Abhijeet Maharana says:

      Hi Sandun, I wish I could help. But I haven’t had a chance to work more with Smack. I would suggest that you try out the forums.

    58. Aruna says:

      on my server java 1.3 is installed, and I need smack and other jar files with java 1.3 … can you help me in this scenario?

    59. Abhijeet Maharana says:

      Hi Aruna,
      I haven’t looked but igniterealtime should have a download archive somewhere that could contain the earlier releases.

    60. Earlence Fernandes says:

      Hi, Your article was very informative.

      I was trying to accomplish a File Transfer using the smack extensions provided by the library. I used the example contained in the doc, but that doesn’t seem to be working properly. On the sender side, my program prints out the msg that File transfer started, When I use the debugging interface for the second app (i.e the one to which i am sending a file to), no event is received.
      Also, in the first app, the enhanced debugger spits out the foll error

      “IQ type not understood”

      I examined the packets send by the first app, it shows that the first type of packet in the File Transfer is of type IQ=set

      Any help would be much appreciated..

      Thank you for your time.

      Cheers,
      Earlence Fernandes

    61. Abhijeet Maharana says:

      Hi Earlence. I didn’t have a chance to work more on this. I am afraid I can’t be of any help here. You could try the Ignite Realtime forums.

    62. Peetamber says:

      hi dude
      Can u help me creating a XMPP Client for monile in j2me???

    63. Prosenjeet says:

      Hi Abhijeet,
      I tried your code snippet, but unfortunately it gives he XMPP connect error.
      XMPPError connecting to talk.google.com:5222.: remote-server-error(502) XMPPError connecting to talk.google.com:5222.
      Although I am trying this from my office PC, yet I have got direct connection to the internet and no proxy involved. I am using Linux RHEL 4 along with Java 5.
      I have also tried a gtalk-eclipse plugin, and got the same error there.
      Can you please help.

    64. Prosenjeet says:

      Hi Abhijeet,

      The whole stack trace is:
      XMPPError connecting to talk.google.com:5222.: remote-server-error(502) XMPPError connecting to talk.google.com:5222.
      — caused by: java.net.ConnectException: Connection timed out
      at org.jivesoftware.smack.XMPPConnection.connectUsingConfiguration(XMPPConnection.java:900)
      at org.jivesoftware.smack.XMPPConnection.connect(XMPPConnection.java:1415)
      at com.jeet.test.SampleGTalkUtil.login(SampleGTalkUtil.java:23)
      at com.jeet.test.SampleGTalkUtil.main(SampleGTalkUtil.java:62)
      Nested Exception:
      java.net.ConnectException: Connection timed out
      at java.net.PlainSocketImpl.socketConnect(Native Method)
      at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:333)
      at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:195)
      at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:182)
      at java.net.Socket.connect(Socket.java:507)
      at java.net.Socket.connect(Socket.java:457)
      at org.jivesoftware.smack.proxy.DirectSocketFactory.createSocket(DirectSocketFactory.java:28)
      at org.jivesoftware.smack.XMPPConnection.connectUsingConfiguration(XMPPConnection.java:888)
      at org.jivesoftware.smack.XMPPConnection.connect(XMPPConnection.java:1415)
      at com.jeet.test.SampleGTalkUtil.login(SampleGTalkUtil.java:23)
      at com.jeet.test.SampleGTalkUtil.main(SampleGTalkUtil.java:62)

    65. Abhijeet Maharana says:

      @Prosenjeet @Peetamber
      Unfortunately I have been out of touch with this for quite sometime and caught up with other things at the moment. I am afraid I can’t be of much help here. Do try the forums though.

    66. Sri says:

      Hi,
      Can we connect to other messengers(yahoo, MSN, AOL and etc..) using smack API? if yes can you provide me useful resources.

      Thanks
      Sri

    67. arun prabhath says:

      I executed this code and I got Authentication failed… What would be the reason? I used my gmail account username and password

    68. ananth says:

      I tried writing the program from scratch I am getting this exception.Please help me figure it out.
      Exception in thread “main” SASL authentication failed using mechanism PLAIN:
      at org.jivesoftware.smack.SASLAuthentication.authenticate(SASLAuthentica
      tion.java:325)
      at org.jivesoftware.smack.XMPPConnection.login(XMPPConnection.java:395)
      at org.jivesoftware.smack.XMPPConnection.login(XMPPConnection.java:349)
      at test.main(test.java:11)

    69. Sandeep says:

      Hi Ananth,

      You need to specify full username including ‘gmail.com’ at the end,then you can avoid that error.

    70. xSolutions says:

      Hi Sandeep,

      I use the full username (with the correct password) and I keep getting the same exception ananth has.

      SASL authentication failed using mechanism PLAIN: … (etc)

    71. Hamy says:

      James,

      For GTalk connections, I seem to be required to add the @gmail.com to the username, like so
      // provide your login information here
      c.login(“madnesstestuser@GMAIL.COM”, “password”);

      best luck,
      hamy

    72. Prateek Sharma says:

      Hello,

      I tried all three ways

      1. c.login(“sprateek007″, “password”);
      2. c.login(“sprateek007@gmail.com”, “password”);
      3. c.login(“sprateek007@GMAIL.COM”, “password”);

      But I am getting the same error

      Exception in thread “main” SASL authentication failed:
      at org.jivesoftware.smack.SASLAuthentication.authenticate(SASLAuthentication.java:209)
      ………

      Please tell me the possible solution for this problem.

    73. Prateek says:

      Hello Abhijeet,

      Can we use this Java API to update the user status/status message or for sending friend request etc. Please tell me how we can do all these stuff.

      Regards,
      Prateek

    74. Abhijeet Maharana says:

      Hi Prateek, take a look at http://www.adarshr.com/papers/xmpp2. Its packed with information!

    75. Jai says:

      Hi Abhijeet,
      I want to make same program in .NET. Cau u please help me for the same?
      How can I get libraries for .NET?

      Thanks and Regards
      Jai

    76. Abdul says:

      hi Abhijeet,

      i worked with this example. it really worked fine for me. i have someclarifications to made. how can i get the availability status of the user i used like this.
      Presence presence = roster.getPresence(“abdul2782@jabber.org”);
      if (presence.getType() == Presence.Type.AVAILABLE) {
      // do something
      }.
      but it doesn’t worked for me. also i tried to take status like
      presence.getStatus();but it shows always null for all the users even though the user is online.

      Also one more thing how can i get chat history

    77. Serj says:

      I try this example but does’t work, i can’t see “Is avaible”, help me …. , thanks !

      ConnectionConfiguration config = new ConnectionConfiguration(“talk.google.com”, 5222, “gmail.com”);
      XMPPConnection connection = new XMPPConnection(config);
      connection.connect();
      connection.login(“userName@gmail.com”, “password”);
      return connection;
      Roster rosterr = connection.getRoster(); //contacts
      Presence presence1 = rosterr.getPresence(“My friend of cantact@gmail.com“);
      System.out.println(presence1.getMode());

    78. Debasish Mandal says:

      Thank u very much its working nicely………
      Thanks for sharing such a useful code.

    79. Vivek says:

      Thanks Abhijeet,i was looking for same code which you have provided.Wonderfull job.Now i can create a google bot .

    80. Arijit says:

      Hi!
      How can i extract the raw xml packet?
      (packet.toXML method hides most part of server reply)
      Now I’m working on gmail notification.In debugger window(raw received packets)I can see
      the “result” packet.

      output of packet.toXML :

      in Raw Received Packets : <mail-thread-info tid=”13
      ….
      ….

      Help Me !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

    81. Swarnabh says:

      hi abhijeet i tried out your code…..it works fine when used with console …..but when i tried it out along with a chat window …..the XMPP Debugger window appears partially and the program hangs up ….please help me to sort out this problem

    82. Junaid IIUI says:

      how to set proxy server ,in Netbeans while using this API.
      i got exception

      Exception in thread “main” Could not connect to talk.google.com:5222.: remote-server-timeout(504) Could not connect to talk.google.com:5222.
      — caused by: java.net.UnknownHostException: talk.google.com

    83. Abhijeet Maharana says:

      swarnabh… a random stray thought from the top of my head … I think the UI thread is being locked up somewhere. Check that out and create threads as needed.

    84. kapil says:

      i tried your example its worked partially fine for me.i can send chat with this but unable to receive proprlly.some time its show null then msg received it shows.but most of tyhe time it doesent receive any msg or if it received its not displaying ?please help

    85. Soham Sengupta says:

      Hi,
      I have developed a nearly full-functionality Universal Chat Client which lets one connect to an arbitrary server, and service.I want to get it distributed to everyone whoever may be interested to develop further on it. You can find my profile:
      http://www.ibm.com/developerworks/spaces/JavaCommBio

    86. Abdul-salam says:

      Downloading now. Great work! Thanks!

    87. Marijo says:

      Sorry, I’m trying to download your project with instructions on compiling and running, but the download link isn’t available any more.

      Is there a way you could send me that project? Or upload it again somewhere?

    88. Abhijeet Maharana says:

      Hi Marijo,

      Thanks for pointing that out. Unfortunately, I didn’t have a copy of the zip I had uploaded to GeoCities. So I googled around to see if the zip is available somewhere and sure enough!

      http://groups.google.com/group/jujuyjug/files?pli=1

      But I don’t remember if I had posted a NetBeans project. The search also led me to a couple of other blogs with a strikingly similar post :)

    89. Kartik says:

      Hi Abhijeet,

      I have downloaded your project and run it successfully. But when I run it, a GUI gets opened. Is there anyway in which I can just implement this messenger in text mode only. Like during the execution of binary, I need to enter the message on standard input console, instead of entering the same in GUI.

      I need to develop a sms chat solution, in which my application will receive SMS from user and will send the same to his friend on gtalk. so for this my need is to have a gtalk messenger client without GUI.

      Thanks in advance for your help.
      BR,
      Kartik

    90. Abhijeet Maharana says:

      I have already linked to the single-file text mode program in the post itself.
      http://abhijeetmaharana.com/blog/bloguploads/gtalkclient/ChatClient.java.txt

    91. Ravi says:

      Hi Abhijeet,
      Thasnk for your work. I am using Smack library for communication between two of my server machines. One Server is Google App Engine(Will call it GAE) server and another is my mocal machine.
      I send a message from my local machine to GAE and it reaches there and then server respond it by saying ok i received it and processed it. Then client machine receive this message.
      Now the problem is that almost 30-40% messages are not being received my Client machine which is using smack library. I check the server logs and it always send messages without error.I tried to send the same message with Gtalk and i always receieve response in Gtalk Chat window, but with smack library i keep loosing my response messages.
      DO you know what is wrong here.

    92. Saravanan says:

      Hi Abhijeet
      I am sending message to my friend from my client program.My friend can get message, but message is not displayed in gtalk window for me.Please help me,

    93. chaitu says:

      Hi abhijeet,
      your ‘download the project’ link is invalid. And also i am able to send message to a buddy but when he replies the listener is unable to listen, is there any common mistake i am doing.
      thanks for your time,
      chaitu

    94. Abhijeet Maharana says:

      Thanks for pointing out. I have updated the post. Try running from the new link.

    95. sampath kumar says:

      hii abhijeet, thanks 4 your project,

      As we r running your code in the IBM websphere server,we couldnt retrive the messages (because of two jvm running at the same time).

      Please give us solution for this error.

    96. Hardik says:

      How can i create send Receive iq packets using smack api

    97. Hardik says:

      How can i create, send and Receive iq packets using smack api
      i am already connected to xmpp server? is any error in packet format can u give me source code?
      i tried everywhere from ignite to jabber.org but not got answer !!
      folowing is packer format

      final IQ iq1 = new IQ() {
      public String getChildElementXML() {
      return “”;
      }
      };

    98. amit.dm says:

      hey
      # Abhijeet Maharan

      i really impressed by ur idea ……….

      i haven’t tried this but seems great .

      im trying to implement gtalk in web browser for my final year project will ur program will help me for gtalk in screenshot i saw call option will it help…….

      please i want directly contact u please send me ur contact details asdfg3551.u@gmail.com….please reply soon

    Leave a Reply