Problem with Java RMI Proxy

I get this error:

 java.lang.ClassCastException: $Proxy0 cannot be cast to rmi.engine.Call
        at Main.main(Main.java:39)

My Abstractand Callclass are expanding 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";
    }
} 

Annotation:

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

    System.out.println(((Call)ss).getHello());

The result is as follows:

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 run the wizard in cmd listening on port 5225.

+3
source share
1 answer

The class $Proxy0is a dynamic proxy i.e. the class is generated by java at runtime.

This class should implement the interface Abstract, but does not extend Call. Thus, you cannot reset it to Call(this gives a cool fit), but its decrease to Abstractshould be in order.

RMI, .

PS: Abstract - . , . , . rmi.engine, - org.myproject, JDK.

+4

Source: https://habr.com/ru/post/1745160/


All Articles