Java – “when creating a simple RMI application,” ClassCastException: $proxy0 cannot be converted “error

I am creating my first very simple RMI client - server application

This is the code:

Interface "communication"

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; 

}

Interface implementation "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";
    }

}

This is my main server class "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'.");

    }

}

This is my client "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);


    }

}

Now I try to run it:

>I use the terminal to navigate to the bin directory of my project. From there, I run rmiresistry > I run my communicationserver from a new terminal window (it prints out messages to make it seem to work) > I open the third terminal window. When I try to run my communicationclient, it throws an exception

So far, I've tried to fix it by using RMIC to create a stub of a "communicationimpl" object, but now instead of '$proxy0', the error refers to 'communicationimpl'_ Stub’:

At this point, I don't know what to look for Can anyone give me any advice?

Solution

replace

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

with

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

Communicationimpl is the server implementation of icommunication The client neither knows nor cares about the implementation, only the interface

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>