Java Programming on the Mac
by Michael Brewer
A Rendezvous with Java
During WWDC this year, Apple released Java source for Rendezvous. You can now write Java applications that can call on a Rendezvous service discovery on Mac OS X, Windows, and any POSIX system using code supplied by Apple. Apple included the Java libraries in the Rendezvous for Windows Technology Preview. Everyone else needs to get the source from Apple to build the Java libraries.
In order to get the source, you need to connect to Apple's Darwin CVS server. Apple's CVS how-to was written for TCSH and needs updating, so don't bother with it. But I'll walk you through it -- don't worry. The very first thing you need to do is to agree to the Apple Public Source License (APSL) while logged in with your Apple ID. Once you have the legalese taken care of, launch Terminal and type in the following two commands:
export CVS_RSH=ssh export CVSROOT=:ext:email@example.com:/cvs/apsl
Open a new TextEdit (or any other text editor you prefer) window and paste the following into the document:
checkout -P update -P -d diff -u -d -b -w rdiff -u cvs -z3
If you're using TextEdit, make sure the document is plain text (opposed to the RTF option). Now save this file as ~/.cvsrc as you can see in the screenshot. Select the Use "." button from the resulting window. The text editor may save the file with a .txt extension -- you can find out by typing
ls -A in Terminal. If this is the case,
mv .cvsrc.txt .cvsrc will remedy it. Entering
man cvs into Terminal will provide you with all of the information you need on these commands -- I'll leave that bit of research up to you in the interest of brevity.
Finder protects you from yourself -- this time it's OK.
You're now ready to check out the source. Type
cvs co mDNSResponder into Terminal. You may get a message stating that the authenticity of "anoncvs.opensource.apple.com" can't be established and asking if you're sure you want to continue.
I can't find any documentation from Apple on what their RSA key fingerprint should be, so I had to assume I was not the victim of a man-in-the-middle attack and reply
yes to this prompt. You will only see this prompt the first time you connect. At the prompt for your password, enter your Apple ID and its passphrase separated by a colon (e.g. firstname.lastname@example.org:insanelygreat) as the password. If you entered the right password, your Terminal will be spammed with a listing of all of the resources being checked out and they will be copied to ~/mDNSResponder/.
|Compiling the Java libraries in Xcode.|
Back in Finder, open the mDNSResponder folder and the mDNSMacOSX subfolder. Open the mDNSResponder.pbproj file -- launching Xcode. Expand the Targets section with the disclosure triangle and select the dns_sd.jar and libjdns_sd.jnilib targets, then choose Build from Xcode's Build menu. This will compile the targets to a build folder in mDNSMacOSX, open the mDNSResponder/mDNSMacOSX/build folder from Terminal, copy dns_sd.jar into /Library/Java/Extensions/, and put libjdns_sd.jnilib into /usr/lib/java/.
sudo cp dns_sd.jar /Library/Java/Extensions/
sudo cp libjdns_sd.jnilib /usr/lib/java/
Still inside Terminal, go to the ~/mDNSResponder/Clients/Java/ folder and type
javac SimpleChat.java. This will create the SimpleChat.class file along with TargetListModel.class, TargetListElem.class, SwingQueryListener.class, SwingBrowseListener.class, SimpleChat$1.class, and ListenerThread.class. Once those files are compiled, you can double-click on SimpleChat.class to run the application.
Great, it runs. But chatting by yourself is no fun. You need to run SimpleChat somewhere else before Rendezvous can swing into action. If you don't have a separate computer to test out the program, you can always use fast user-switching. Fast user-switching might actually be preferable in this case because if you're using a separate computer, you need to make sure you have the
libjdns_sd.jnilib files in place on it as well. Make sure you have the application in a place accessible by both users. I used my ~/Public folder. Login as another user and then navigate to the first user's Public folder and open SimpleChat.class.
Hmmm, something went wrong.
What is this error? Reading through this article and following the steps, it is sometimes easy to overlook some details. If you saw that same error, you forgot to copy all of the other class files to Public along with
SimpleChat.class. Now you either need to go back to your first computer or your first user and copy the rest of the files. When all of the files are there, SimpleChat will run and you can chat to yourself or your own willing Mr. Watson.
The first transmission.
There has got to be a better way of distributing the application, though, right? There is. Apple has wisely provided Jar Bundler. Jar Bundler will take all of your Java application's resources and bundle them up into a Macintosh application bundle. This means that the Java application will only execute in a Macintosh environment, but it's a good way to tailor a distribution of all of your cross-platform Java code into a more Mac-like experience.
Open Jar Bundler from /Developer/Applications/Java Tools/. Choose SimpleChat.class as the Main class for the bundle. Check the "Use Macintosh Menu Bar" and "Disable .app Package Navigation" checkboxes. I'm also using the Fire icon from Brandon's Chat Icons. You could let Jar Bundler use Apple's default Java application icon, but this gives SimpleChat a bit of style.
Packaging up the Java application.
Switch to the Classpath and Files tab, and add all of the other class files -- unfortunately you have to add them individually. I've found that it makes it a bit easier to find the class files when you sort the file list by Date Modified in descending order.
Change to the Properties tab and set the Version to
1.0, Identifier to
com.apple.SimpleChat, Get-Info String to SimpleChat 1.0, and Bundle Name to
SimpleChat. You're finally ready to select Create Application -- name it
SimpleChat. Now you can easily distribute SimpleChat, assuming that the Java Rendezvous libraries are already installed on the destination Mac.
Congratulations! You've just rolled Java Rendezvous libraries and built a Rendezvous enabled chat client from open source code, while learning how you can deploy this code -- usually reserved for geeks -- in an attractive package.
With Apple’s gift to the community in the form of these Java libraries, I can't wait to see some cross-platform, Rendezvous-enabled applications out there. It'll be great to be able to enjoy a wide array of zero-configuration networking with a larger pool of people than just other Mac users.
Michael Brewer is a developer based near Charlotte, North Carolina. His interests include web development of various flavors, databases, and Java. One of the off-shoots of these activities is his website Brewed Thoughts.
Return to MacDevCenter.com.