【发布时间】:2015-01-30 02:53:54
【问题描述】:
我正在尝试使用 RMI 在远程服务器上调用斐波那契方法,但是当我尝试通过给方法一个整数值来调用客户端的方法时,我收到以下错误:
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;
}
【问题讨论】:
-
注意:描述和回答问题所需的所有信息都必须包含在问题本身中。您提供的 pastebin 链接是在浪费时间。
-
@RambabuMandalapu 删除的答案确实是一个答案。只是一个错误的答案。奇怪的评论。
标签: java client-server rmi