javarmirmiregistry

Run java application without starting rmiregistry


When I'm running a client-server application that use Java RMI, I noticed that it still seems to work even though if I didn't start "rmiregistry" in the command prompt that I was learnt to do. The diffrence in the program I noticed:

    try { 
        LocateRegistry.getRegistry(1099).list();
        System.out.println("rmiregistry WAS started before running the application"); } 
    catch (RemoteException e) {
        LocateRegistry.createRegistry(1099);
        System.out.println("rmiregistry WAS NOT started before running the application"); }

Anyone with the knowledge could sort out what's happening and how it effect the program?


Solution

  • LocateRegistry.getRegistry(1099).list();
    System.out.println("rmiregistry WAS started before running the application"); }
    

    That message is misleading. getRegistry() doesn't perform any network communication, so the fact that it succeeded isn't a valid indication that the Registry is running. Only using the result of getRegistry() can tell you that.

    You're doing this back to front. You must try the createRegistry() call first, and if that fails do the getRegistry() call.createRegistry()` will fail if the Registry is already running, and succeed if it isn't.