我找到了一种安全的获取方式LocateRegistry
(即使注册表尚不存在)。
Registry registry = null;
try {
registry = LocateRegistry.getRegistry(52365);
registry.list();
// This call will throw an exception if the registry does not already exist
}
catch (RemoteException e) {
registry = LocateRegistry.createRegistry(52365);
}
是否可以先检查注册表的存在和使用情况getRegistry
或createRegistry
根据检查结果?