java – “创建简单的RMI应用程序时,”ClassCastException:$Proxy0不能被转换“错误

我正在创建我的第一个非常简单的RMI客户端 – 服务器应用程序.

这是代码

界面“通讯”

package itu.exercies.RMI.server;

    import java.rmi.Remote;
    import java.rmi.remoteexception;

public interface ICommunication extends Remote  
{
    public String doCommunicate(String name) throws remoteexception; 

}

界面实现“CommunicationImpl”:

package itu.exercies.RMI.server;

import java.rmi.remoteexception;
import java.rmi.server.UnicastRemoteObject;

public class CommunicationImpl extends UnicastRemoteObject implements ICommunication {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    public CommunicationImpl() throws remoteexception {
        super();

    }

    @Override
    public String doCommunicate(String name) throws remoteexception {

        return "Hello this is server,whats up " +name+ " ?!\n";
    }

}

这是我的主要服务器类“CommunicationServer”:

package itu.exercies.RMI.server;

import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.remoteexception;


public class CommunicationServer {
    /**
     * @param args
     * @throws remoteexception 
     * @throws MalformedURLException 
     */
    public static void main(String[] args) throws remoteexception,MalformedURLException {
        CommunicationImpl imp = new CommunicationImpl();
        Naming.rebind("CommunicationImpl",imp);
        System.out.println("Server started...");
        System.out.println("Object successfully registered. It is bound to the name 'CommunicationImpl'.");

    }

}

这是我的客户端“CommunicationClient”:

package itu.exercies.RMI.client;

import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.remoteexception;

import itu.exercies.RMI.server.CommunicationImpl;

public class CommunicationClient {
    public static void main(String[] args) throws MalformedURLException,remoteexception,NotBoundException {

        String url = new String("rmi://localhost/CommunicationImpl");
        CommunicationImpl comm = (CommunicationImpl)Naming.lookup(url);
        String reply = comm.doCommunicate("Wiktor");
        System.out.println(reply);


    }

}

现在我试图运行它:

>我使用终端导航到我的项目的bin目录
我从那里经营rmiregistry
>我从新的终端窗口运行我的CommunicationServer(它打印出的消息,使其似乎工作)
>我打开第三个终端窗口,当我尝试运行我的CommunicationClient时,它会抛出异常.

java itu.exercies.RMI.client.CommunicationClientException in thread “main” java.lang.classCastException: $Proxy0 cannot be cast to itu.exercies.RMI.server.CommunicationImpl
at itu.exercies.RMI.client.CommunicationClient.main(CommunicationClient.java:14)

到目前为止,我试图通过使用rmic创建一个“CommunicationImpl”对象的存根来修复它,但现在而不是’$Proxy0′,错误是指’CommunicationImpl_Stub’:

Exception in thread “main” java.lang.classCastException: itu.exercies.RMI.server.CommunicationImpl_Stub cannot be cast to itu.exercies.RMI.server.CommunicationImpl
at itu.exercies.RMI.client.CommunicationClient.main(CommunicationClient.java:14)

在这一点上,我不知道要查找错误.有人可以给我任何建议吗?

解决方法

更换
CommunicationImpl comm = (CommunicationImpl) Naming.lookup(url);

ICommunication comm = (ICommunication) Naming.lookup(url);

CommunicationImpl是ICommunication的服务器实现.客户端既不知道也不关心实现,只有接口.

相关文章

最近看了一下学习资料,感觉进制转换其实还是挺有意思的,尤...
/*HashSet 基本操作 * --set:元素是无序的,存入和取出顺序不...
/*list 基本操作 * * List a=new List(); * 增 * a.add(inde...
/* * 内部类 * */ 1 class OutClass{ 2 //定义外部类的成员变...
集合的操作Iterator、Collection、Set和HashSet关系Iterator...
接口中常量的修饰关键字:public,static,final(常量)函数...