我收到此错误:Java RMI的代理问题
java.lang.ClassCastException: $Proxy0 cannot be cast to rmi.engine.Call
at Main.main(Main.java:39)
我Abstract
和Call
类都扩展Remote
。
电话:
public class Call extends UnicastRemoteObject implements rmi.engine.Abstract {
public Call() throws Exception {
super(Store.PORT, new RClient(), new RServer());
}
public String getHello() {
System.out.println("CONN");
return "HEY";
}
}
摘要:
public interface Abstract extends Remote {
String getHello() throws RemoteException;
}
这是我的主:
public static void main(String[] args) {
if (args.length == 0) {
try {
System.out.println("We are slave ");
InetAddress ip = InetAddress.getLocalHost();
Registry rr = LocateRegistry.
getRegistry(ip.getHostAddress(), Store.PORT, new RClient());
Object ss = rr.lookup("FILLER");
System.out.println(ss.getClass().getCanonicalName());
System.out.println(((Call)ss).getHello());
} catch (Exception e) {
e.printStackTrace();
}
} else {
if (args[0].equals("master")) {
// Start Master
try {
RMIServer.start();
} catch (Exception e) {
e.printStackTrace();
}
}
Netbeans的说,问题是上线39这是
System.out.println(((Call)ss).getHello());
输出看起来是这样的:
运行:
We are slave
Connecting 10.0.0.212:5225
$Proxy0
java.lang.ClassCastException: $Proxy0 cannot be cast to rmi.engine.Call
at Main.main(Main.java:39)
BUILD SUCCESSFUL (total time: 1 second)
我运行在cmd中主侦听端口5225.
真的对我有帮助。 因为我还创建了服务器实现对象而不是接口。 您应该记住,无论您何时调用服务器程序,即远程方法,都应该通过定义的接口调用它们。 – 2012-04-09 06:09:18