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。
I am getting this error:
java.lang.ClassCastException: $Proxy0 cannot be cast to rmi.engine.Call
at Main.main(Main.java:39)
My Abstract
and Call
class both extend Remote
.
Call:
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";
}
}
Abstract:
public interface Abstract extends Remote {
String getHello() throws RemoteException;
}
This is my main:
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 says the problem is on line 39 which is
System.out.println(((Call)ss).getHello());
The output looks like this:
Run:
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)
I am running a master in cmd listening on port 5225.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
类
$Proxy0
是一个 动态proxy,即java在运行时生成的一个类。该类应实现接口
Abstract
,但不扩展Call
。因此,您不能将其向下转换为 Call (这提供了类转换),但将其向下转换为 Abstract 应该没问题。如果使用 RMI,则只能通过接口与远程对象进行通信。
PS:
Abstract
是一个令人困惑的名字。我真的以为你在谈论 抽象类第一的。如果可能的话我会重命名它。与rmi.engine
相同,更喜欢org.myproject
之类的东西,以避免与JDK的内部类混淆。The class
$Proxy0
is a dynamic proxy, that is, a class is generate by java at run-time.This class should implement the interface
Abstract
, but does not extendCall
. So you can't downcast it toCall
(this gives a class cast), but downcasting it toAbstract
should be ok.If you use RMI, you should only communicate with remote object through interfaces.
PS:
Abstract
is kind of a confusing name. I really thought that you were speaking of abstract class first. I would rename that if possible. Same forrmi.engine
, prefer something likeorg.myproject
, to avoid confusion with internal class of the JDK.