Our class has just started learning about RMI. Many of the tutorials I’ve looked at are a bit out of date, however. For instance, many of them still mention generating stubs and/or skeletons using the rmic
command, but this is no longer necessary as of jdk 1.5. All you have to do now is compile your classes and stub files are created dynamically at runtime (skeletons are obsolete in 1.5). Of course, if you are working with a pre 1.5 jdk, you’ll have to do things the old way. Something else I noticed was that most of the tutorials had me run rmiregistry
from the directory where my remote classes resided. I had to do this before running my application. This seemed a bit clunky to me, so I investigated (read Googled) for a better solution. I found it. Rather than running this process from a command console, use java.rmi.registry.LocateRegistry
.
try { Calculator c = new CalculatorImpl(); // These next two lines are the key lines Registry registry = LocateRegistry.createRegistry(1099); registry.rebind(“//localhost/CalculatorService”, c ); Naming.rebind(“rmi://localhost:1099/CalculatorService”, c); } catch (Exception e) { System.out.println(“Trouble: ” + e); } |
The two lines just below the comment are the important ones. They save you having to start the rmiregistry
process each time before you run your program.