1

I'm trying to get a RMI program to work. So far, the server starts up correctly but the client fails casting the remote object to the interface.

Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: com.sun.proxy.$Proxy0 cannot be cast to MonitorClient.InterfaceMonitor

All other answers I've found are for cases where the end user has cast the equivalent of InterfaceMonitorImpl (unknown to the client) instead of the Interface instead. This is not my case and I'm really at a loss here — RMI is nightmare-ish.

Server side

Main:

    InterfaceMonitor obj;
    try {
        LocateRegistry.createRegistry(1099);

        InterfaceMonitor stub = (InterfaceMonitor) UnicastRemoteObject.exportObject(new InterfaceMonitorImpl(), 0);

        Registry registry = LocateRegistry.getRegistry();
        registry.bind("imon", stub);

        System.out.println("Server ready");
    } catch (RemoteException | AlreadyBoundException ex) {
        System.out.println("Server error: " + ex.toString());
    }

InterfaceMonitor.java:

public interface InterfaceMonitor extends Remote {
    int checkAge() throws RemoteException; 
}

InterfaceMonitorImpl.java:

public class InterfaceMonitorImpl implements InterfaceMonitor {

    public InterfaceMonitorImpl() throws RemoteException {

    }

    @Override
    public int counter() throws RemoteException {
        return 10;
    }

}

Client side

    try {
        Registry reg = LocateRegistry.getRegistry(null);
        InterfaceMonitor im = (InterfaceMonitor) reg.lookup("imon");

        int counter = im.counter();
        System.out.println("Counter: " + counter);
    } catch (NotBoundException | RemoteException ex) {
        Logger.getLogger(MonitorGUI.class.getName()).log(Level.SEVERE, null, ex);
    }

The InterfaceMonitor.java is also on the client side.

Thanks for your time!

1
  • It has to be the same interface not just an interface with the same name and methods. Also is this the code as used? The interface has a checkAge method whereas the implementattion has a counter method. Commented May 22, 2014 at 10:48

1 Answer 1

2

Obviously you must have two copies of InterfaceMonitor: one in MonitorClient and one in what may be something like MonitorServer. That makes two different classes. Not two copies of the same class. The class name, package, method declarations, inheritance, ... all have to be the same.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks! Now I'm getting a "java.rmi.NoSuchObjectException: no such object in table" error, after cross-importing the InterfaceMonitor from the server package (not happy with it, but it can be run locally as a demo)
That means that the remote object has been unexported. The solution is to store the result of LocateRegistry.createRegistry(1099) into a static variable.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.