Search code examples
javarmi

RMI client com.sun.proxy.Proxy cannot be cast error


Server:

package server;

import java.rmi.*;
public interface iCSServer extends Remote
{
    public int Findnumber(int num) throws RemoteException;
}


package server;

import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.server.UnicastRemoteObject;

And

public class CSServer extends UnicastRemoteObject implements iCSServer {
    private static final long serialVersionUID = 01; // version 01
    int N=1000; // number of ids
    int[] Ids=new int[N]; // data structure for storing the Ids
    public CSServer() throws RemoteException
    {
    }
    public void Add_Ids() // add ids to data
    {
        for(int i=0;i<N;i++)
        {
            Ids[i]=i+ (i+1)*10;
        }
    }
    public int Findnumber(int num) throws RemoteException
    {
        for(int i=0;i<N;i++)
        {
            if(num==Ids[i])
            {
                return i;
            }
        }
        return -1;
    }
    public static void main(String[] args) throws RemoteException {

        System.setProperty("java.rmi.server.hostname","127.0.0.1");
        LocateRegistry.createRegistry(1099);


       // System.out.println(LocateRegistry.getRegistry(1099).toString());

        CSServer obj=new CSServer();
        obj.Add_Ids();

        try {
            Naming.rebind ("CSServer", obj);
            System.out.println ("CS218 Server is running.");
        }
        catch (Exception e) {
            System.out.println ("CS218 server cannot run: " + e);
        }
    }
}

Server starts and runs fine.

Client:

package client;

import java.rmi.Remote;
import java.rmi.RemoteException;
import java.rmi.*;

public interface iCSServer extends Remote
{
    public int Findnumber(int num) throws RemoteException;
}

And

package client;

import java.rmi.Naming;
import java.rmi.registry.*;
public class clientApp {
    public static void main(String[] args)
    {



        try{
            iCSServer obj = (iCSServer) Naming.lookup("//localhost:1099/CSServer");
          //Registry registry = LocateRegistry.getRegistry("127.0.0.1");
          //iCSServer obj=(iCSServer) registry.lookup("CSServer");

            System.out.println("pass");
            System.out.println(obj.Findnumber(100));
        }
        catch(Exception ex)
        {
            System.out.println("fail");
            System.out.println(ex.getMessage());
        }
    }
}

However, when I run the client I get this error:

fail
error unmarshalling return; nested exception is: 
java.lang.ClassNotFoundException: server.iCSServer (no security manager: RMI class loader disabled)

then I added a jar'd version of server.ICSServer, I believe that fixed that issue. However, I ran it again and now I get this error:

fail
com.sun.proxy.$Proxy0 cannot be cast to client.iCSServer

Any ideas on how to fix?


Solution

  • You have renamed the interface by putting it into a different package. It has to be the same.