Java RMI 代理问题

发布于 2024-09-01 16:05:00 字数 1891 浏览 4 评论 0原文

我收到此错误:

 java.lang.ClassCastException: $Proxy0 cannot be cast to rmi.engine.Call
        at Main.main(Main.java:39)

我的 AbstractCall 类都扩展了 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

残花月 2024-09-08 16:05:00

$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 extend Call. So you can't downcast it to Call (this gives a class cast), but downcasting it to Abstract 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 for rmi.engine, prefer something like org.myproject, to avoid confusion with internal class of the JDK.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文