Search code examples
javarmistub

Java RMI - automate creation / copy of stubs and start of registry


I looked at tutorials for Java RMI and they suggest to do the mentioned tasks above manually in the command line (Windows). In detail that means:

  • set the path of the server project to the jdk/bin folder

  • use rmic "ClassImplementingRemoteInterface"

  • start rmiregistry

  • copy stub file to client project's bin folder

I found, that it is possible to start the registry by adding the following code to the main method of the sever.

       `try {
            java.rmi.registry.LocateRegistry.createRegistry(PORT);
            System.out.println("RMI registry ready.");
        } catch (Exception e) {
            System.out.println("Exception starting RMI registry:");
            e.printStackTrace();
        }
   `

By now, I created the stubs via command line and copied them to the bin folders of the client porject. I am confused how to automate this since the documentation is suggesting, that the manual creation of stubs is not necessary anymore. Oracle Documentation

So is there a more flexible way to create stubs and to move them to the clients?

UPDATE:

To start the registry and to send the remote object, only the methods " java.rmi.registry.LocateRegistry.createRegistry(port) and "UnicastRemoteObject.exportObject(object, port) are needed. They should be implemented within the main method of the server:

  try {


           java.rmi.registry.LocateRegistry.createRegistry(1099);
           Connector Hello = new Connector();

          IConnector stub = (IConnector) UnicastRemoteObject.exportObject(Hello, 1099);

          // System.setProperty("java.rmi.server.hostname","134.155.182.118");
           Naming.rebind("rmi://localhost/ABC", Hello);

           System.out.println("Addition Server is ready.");
           }catch (Exception e) {
               System.out.println("Addition Server failed: " + e);
            }

Solution

  • I am confused how to automate this since the documentation is suggesting, that the manual creation of stubs is not necessary anymore.

    Correct. As long as you satisfy the conditions stated in the preamble to the Javadoc for UnicastRemoteObject, stub objects will be generated dynamically. Basically you always have to specify a port number when constructing or exporting a remote object. You can use zero if you don't care.

    So is there a more flexible way to create stubs and to move them to the clients?

    You don't need to do either. So don't.