Java RMI - 自动创建/复制存根和启动注册表

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

我查看了 Java RMI 的教程,他们建议在命令行中手动执行上述任务 (Windows)。详细意思是:

我发现,可以通过将以下代码添加到服务器的主要方法来启动注册表。

       `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();
        }
   `

至此,我通过命令行创建了存根并将它们复制到客户端项目的bin文件夹中。我很困惑如何自动执行此操作,因为文档建议不再需要手动创建存根。 Oracle Documentation

那么有没有更灵活的方法来创建存根并将它们移动到客户端?

更新:

要启动注册表并发送远程对象,只需要方法" java.rmi.registry.LocateRegistry.createRegistry(port) 和"UnicastRemoteObject.exportObject(object, port)。它们应该在服务器的主要方法中实现:

  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);
            }

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

正确。只要您满足 UnicastRemoteObject 的 Javadoc 序言中规定的条件,就会动态生成存根对象。基本上,在构造或导出远程对象时,您总是必须指定一个端口号。如果您不在乎,可以使用零。

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

你不需要做任何一个。所以不要。