tags:

views:

101

answers:

1

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.

+1  A: 

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.

ewernli
Thank you so much for pointing out the obvious mistake! Lifesaver!
Antony Lewis