Google Analytics

Tuesday, September 27, 2005

How to get Java RMI going on Mac OS X and Linux

For a subject I'm doing at uni I need to make heavy use of Java RMI. I had a lot of problems getting it running, even when following the simplest tutorials so I thought I would write down how I eventually got it running.

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.

11 comments:

smithk@cs.man.ac.uk said...

Thanks for your example - it was a big help.

with regard to the following ...

"$ 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."
...

There is no need to run rmiregistry from the directory "classes" if the following syntax is used ...

java -Djava.rmi.server.codebase=file:/Users/kjsmith/desk/classes/example.hello.Server

(This is all on one line & clearly my path should be substituted by yours).


smithk@cs.man.ac.uk

Kenshin said...

$ 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."

This is indeed very importance part. Thank to your suggestion I managed to run it. Thank very much

Anonymous said...

Hi Mike,

thank's a lot for your tutorial! It helped me very much,

there's just a little typo:
$ java -d classes/ *.java

should be:
$ javac -d classes/ *.java

OuZo said...

Thanks! This was a great help for me!

Milan said...

Thanks a lot! it helped me for my dist sys class! Cheers!

Anonymous said...

much love to you both, Mike and smithk@cs.man.ac.uk! i as at my wit's end with getting this working. phew!

Cornelis said...

Creat help ! Tnx

ZEN said...

Thanks a lot mike!! Ur post, it was a huge help for me....thx.

Gustavo Markel said...

Thanks this was vey very helpful !!!

z95 said...

Thanks, you fucking legend.

Anonymous said...

He is my legend , Ten years and people asses are still saved by this guy Blog , namaste sansai