【发布时间】:2010-05-12 18:41:33
【问题描述】:
我收到此错误:
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 中运行一个 master 监听端口 5225。
【问题讨论】:
-
对我真的很有帮助。因为我也在创建服务器实现的对象而不是接口。您应该记住,无论何时调用服务器程序,即远程方法,您都应该通过定义的接口调用它们。