【问题标题】:Why is Java RMI server showing an error message when I try to run it?为什么当我尝试运行 Java RMI 服务器时显示错误消息?
【发布时间】:2017-04-07 09:23:44
【问题描述】:

程序执行后抛出如下异常。

未解决的编译问题:方法bind(String, Remote) in Registry 类型不适用于参数(字符串, EmployeeRMIMain)

    public static void main(String args[]){
        try {
            EmployeeRMIMain obj = new EmployeeRMIMain();
            Registry r = LocateRegistry.createRegistry(1234);
            r.bind("Remote", obj);
        } catch (RemoteException e) {
            e.printStackTrace();
        } catch (AlreadyBoundException e) {
            e.printStackTrace();
        }
    }

【问题讨论】:

  • 我发现了问题。
  • 没有。以下错误消息打印的,这是而不是程序的执行。
  • 不要编辑您的问题以删除您所询问的问题。使其对其他读者毫无用处:也自相矛盾,因为更正的代码不可能导致显示的错误消息。或者删除你的问题。

标签: java rmi


【解决方案1】:

编辑:用户已经解决了他自己的问题。 @KMuir,即使您找到了解决方案,也要发布解决方案。

EmployeeService 类的类接口是什么?你确定它实现了远程标记接口吗?

public interface TunnelingMessageBox extends Remote {
  public void pushMessage(Message message) throws RemoteException;
  //..more interface methods
}

public class TunnelingMessageBoxImpl implements TunnelingMessageBox {
  public void pushMessage(Message message) throws RemoteException {
     // does the magic
  }
}

public class MyService {
  private Registry registry;
  private int port;
  public void createRegistry(int port) throws RemoteException {
        Registry reg;
        try {
            reg = LocateRegistry.createRegistry(port);
        } catch (ExportException ex) {
            // get existing registry instance.
            reg = LocateRegistry.getRegistry(port);
        }
        this.port=port;
        registry = reg;
  }

  public vooid closeRegistry() {
    try {
      Remote obj = (Remote)registry.lookup("box1");
      UnicastRemoteObject.unexportObject(obj, true);
      registry.unbind("box1");
    } catch(Exception ex) { ex.printStacktrace(); }
    registry=null;
  }

  public void registerServices() throws RemoteException {
    TunnelingMessageBoxImpl mbox = new TunnelingMessageBoxImpl();
    UnicastRemoteObject.exportObject(mbox, port);
    registry.rebind("box1", mbox);
  }

}

【讨论】:

  • 我对最初发布的代码进行了更正。问题是我正在创建错误类的对象。我在上面的代码中创建了这个类的一个对象(EmployeeRMIMain),而不是更新的 EmployeeService 类。发生错误是因为 EmployeeRMIMain 方法没有从以下类 UnicastRemoteObject 继承。
【解决方案2】:

我对最初发布的代码进行了更正。问题是我正在创建错误类的对象。

我正在创建此类 (EmployeeRMIMain) 的对象,而不是上面代码中更新的 EmployeeService 类。发生错误是因为 EmployeeRMIMain 方法没有从以下类 UnicastRemoteObject 继承。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-03-28
    • 1970-01-01
    • 1970-01-01
    • 2014-05-01
    • 2021-02-17
    • 1970-01-01
    • 2015-07-04
    相关资源
    最近更新 更多