调用远程方法时出错

时间:2014-12-02 00:00:09

标签: java client-server rmi

我试图使用RMI在远程服务器上调用fibonacci方法,但是当我尝试通过给方法一个整数值来调用客户端方法时,我得到了这些错误:

java.rmi.ServerException: RemoteException occurred in server thread; nested exception is:
    java.rmi.UnmarshalException: unrecognized method hash: method not supported by remote object
    at sun.rmi.server.UnicastServerRef.dispatch(Unknown Source)
    at sun.rmi.transport.Transport$1.run(Unknown Source)
    at sun.rmi.transport.Transport$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at sun.rmi.transport.Transport.serviceCall(Unknown Source)
    at sun.rmi.transport.tcp.TCPTransport.handleMessages(Unknown Source)
    at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(Unknown Source)
    at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
    at sun.rmi.transport.StreamRemoteCall.exceptionReceivedFromServer(Unknown Source)
    at sun.rmi.transport.StreamRemoteCall.executeCall(Unknown Source)
    at sun.rmi.server.UnicastRef.invoke(Unknown Source)
    at java.rmi.server.RemoteObjectInvocationHandler.invokeRemoteMethod(Unknown Source)
    at java.rmi.server.RemoteObjectInvocationHandler.invoke(Unknown Source)
    at com.sun.proxy.$Proxy0.fibonacciArrayTest(Unknown Source)
    at ie.gmit.FibonacciClient.main(FibonacciClient.java:37)

有没有人知道我在这个实现方面出了什么问题?

这是RMI应用程序的客户端:

      //get user input 
      Scanner user_input = new Scanner(System.in);
      String fibMaxNum;

      System.out.println("Enter the max fibonacci number: ");
      fibMaxNum = user_input.next();


      int fibMax = Integer.parseInt(fibMaxNum);

     //Get Fibonacci array.
     int[] sequence = power_proxy.fibonacciArrayTest(fibMax);
     for (int value : sequence) {
        System.out.println(value);
     }

这是服务器端的实现,我在这里也收到错误The return type is incompatible with IPower.fibonacciArrayTest(int)。我从中收集到我没有在Ipower接口中指定正确的返回类型,但我如何纠正签名来解决这个问题呢?我应该将Ipower中的方法更改为:

   public int[] fibonacciArrayTest(int n) {

        int a = 0;
        int b = 1;
        int[] sequence = new int[n];

        // Fill array with Fibonacci values.
        for (int i = 0; i < n; i++) {
            sequence[i] = a;

            int temp = a;
            a = b;
            b = temp + b;
        }
        return sequence;
        }

界面:

public interface IPower extends Remote{

     //Declare available methods and must throw RemoteException 
      int[] fibonacciArrayTest(int fibMax) throws RemoteException;

}

1 个答案:

答案 0 :(得分:3)

部署后您已更改远程方法签名。

清理并重建并重新部署到客户端和服务器,不要忘记重新启动注册表。