This assumes you're running Java 1.5, which is important as there were significant changes to Java RMI between 1.4 and 1.5.
If you don't have Java 1.5 and you're running OS X 10.4 (Tiger), here's a brief run down on how to get it going:
- Head over to Apple's Java 1.5 download page and download the DMG.
- Open the DMG, run the package inside and follow the instructions.
- While 1.5 is now installed, it has not overwritten the existing 1.4 stuff that comes with OS X 10.4. It also won't be the default. To make it the default you need to change a symlink. Open up Terminal and change the "CurrentJDK" symlink to the 1.5 install in "/System/Library/Frameworks/JavaVM.framework/Versions". That is:
cd /System/Library/Frameworks/JavaVM.framework/Versions; sudo rm CurrentJDK; sudo ln -s 1.5 CurrentJDK;
For Gentoo Linux the 1.5 JDK is actually masked for compatibility reasons. I had no problems unmasking and installing it (i.e. my other Java applications still run OK) but your mileage may vary. Here are the steps:
# echo ">=dev-java/sun-jdk-1.4.99" >> /etc/portage/package.unmask # emerge sun-jdk (follow instructions given by the ebuild) # java-config -S sun-jdk-1.5.0.05 # /usr/sbin/env-update && source /etc/profile(The '#' shell prompt indicates it's the root user.)
For Ubuntu Linux I followed the instructions at https://wiki.ubuntu.com/RestrictedFormats#java.
Now that the 1.5 JDK is installed, let's get a quick, simple (and yes, probably dirty) example going just to prove to yourself that Java RMI can actually work.
Head over to Sun's "Getting Started Using Java RMI" page. Download the three Java files it lists up the top into a directory. Now it's probably quickest to just show shell actions:
$ cd /where/i/download/the/java/files $ ls Client.java Hello.java Server.java $ mkdir classes $ javac -d classes/ *.java $ tree . |-- Client.java |-- Hello.java |-- Server.java `-- classes `-- example `-- hello |-- Client.class |-- Hello.class `-- Server.class 3 directories, 6 files $ cd classes/
Now at this point, bring up three terminals. We need to run the RMI registry in one, the object server in another and an object client.
$ rmiregistry
It is of utmost importance that this command is run in the same directory the package is in, (classes/ in our case). This problem had me confuzzled for hours and hours, as the server would just return vague "class not found" errors.
Now fire up the server in another shell.
$ java example.hello.Server Server ready
Finally, run the client in its own shell.
$ java example.hello.Client response: Hello, world!
Now wipe your brow and go and get some pizza ;)
Some things to note: Only once the RMI registry has been started can the server run (as it will attempt to register an object with it). Similarly, this example code doesn't check whether the object has already been registered with it, so if you try and run the server a second time without restarting the RMI registry it will produce an error.