powered by Jive Software

Applet''s and Smack

I have seen people mention having problems with Smack making connections to chat servers when used in an applet, just want to post a few things I have figured out over the last few days. (as a I side note I think Java Applets should die a slow and horrible death and people should just write normal apps and deploy via Java Web Start…although sometimes, as in my case, we are maintaining existing code so it is a necessary evil for now).

As everyone probably knows by default an applet can only make a Socket connection back to the server it came from due to the security sandbox an applet has to play in. This can be worked around by signing your applet. Then it is up to the user to decide whether they trust your signature. This is complicated further if you kick off your applet by executing a Java method via javascript. Even a signed applet cannot play outside the security box if the privileged action occurs on the same thread as the method executed via javascript, this thread from Sun’'s Java forums has a workaround for that (2nd post down):


My situation called for connecting to a chat server from an applet and the chat server was not running on the web server the applet came from.

What I found was that since the Smack libraries are a 3rd party jar file they didn’‘t get to play outside the security sandbox just because my applet was signed. I also tried signing the smack libraries with my key and it didn’‘t work. I then tried unjarring the smack libraries, jarring them back up and then signing them. Still wouldn’'t connect because of security exceptions.

My final solution was to package up the smack classes in the same jar file containing my applet and then signing the entire thing. This worked!

So unjar your applet classes in a temp directory of some sort and in the same directory unjar smack.jar and delete META-INF/MANIFEST.MF then repeat for smackx.jar. Do not delete the META-INF directory as it contains some other files that smack needs. Simply delete the MANIFEST.MF file.

Delete the original jar files from the temp directory then jar everything back up and then sign the resulting jar file (I did this step with ANT but do it however you want).

This results in an applet that can connect to a chat server that is not running on the same machine the applet came from i.e. the web server. (Assuming the user accepts your signature).

Hope this info can save someone else from a couple of days of trial and error and frustration.