ClassCastException:无法分配com.sun.proxy。$ Proxy0的实例

时间:2019-06-23 12:03:18

标签: java rmi

我正在尝试从中获取带有RMI的对象,但不确定为什么它不起作用。

我检查了是否放置了正确的接口,以及我尝试传递的对象是否实现了Serializable。

我要传递的对象:

public abstract class ServerInformation implements Serializable {

    private String IDENTIFIER, OS, OS_VERSION, PROCESSOR_NAME;

    protected ServerInformation(String identifier, String os, String os_version, String processor_name) {
        IDENTIFIER = identifier;
        OS = os;
        OS_VERSION = os_version;
        PROCESSOR_NAME = processor_name;
    }

    public abstract double getRamUsed();

    public abstract double getMaxRam();

    public abstract double getStorageUsed();

    public abstract double getMaxStorage();

    public String getIDENTIFIER() {
        return IDENTIFIER;
    }

    public String getOS() {
        return OS;
    }

    public String getOS_VERSION() {
        return OS_VERSION;
    }

    public String getPROCESSOR_NAME() {
        return PROCESSOR_NAME;
    }
}

我的界面:

public interface ClientRemoteInterface extends Remote {

    ServerInformation getInformation() throws RemoteException;

}

出现错误的地方:

    public void update() {
        if (canUpdate) {
            try {
                for (String id : servers) {
                    ClientRemoteInterface client = (ClientRemoteInterface) Naming.lookup(String.format("rmi://%s:%s/HCoreServer_%s_Client", host, port + "", id));
                    ServerInformation info = client.getInformation();// This line gives me the error!

                    // OTHER CODE
                }
            } catch (NotBoundException | MalformedURLException | RemoteException e) {
                e.printStackTrace();
            }
        }
    }

我希望得到我想要的对象,但是却出现了这个错误:

Exception in thread "Timer-0" java.lang.ClassCastException: cannot assign instance of com.sun.proxy.$Proxy0 to field me.WaffleMast3r.HCore.HCoreServer.Server$2.this$0 of type me.WaffleMast3r.HCore.HCoreServer.Server in instance of me.WaffleMast3r.HCore.HCoreServer.Server$2
        at java.io.ObjectStreamClass$FieldReflector.setObjFieldValues(ObjectStreamClass.java:2287)
        at java.io.ObjectStreamClass.setObjFieldValues(ObjectStreamClass.java:1417)
        at java.io.ObjectInputStream.defaultReadFields(ObjectInputStream.java:2293)
        at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:2211)
        at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:2069)
        at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1573)
        at java.io.ObjectInputStream.readObject(ObjectInputStream.java:431)
        at sun.rmi.server.UnicastRef.unmarshalValue(UnicastRef.java:322)
        at sun.rmi.server.UnicastRef.invoke(UnicastRef.java:174)
        at java.rmi.server.RemoteObjectInvocationHandler.invokeRemoteMethod(RemoteObjectInvocationHandler.java:227)
        at java.rmi.server.RemoteObjectInvocationHandler.invoke(RemoteObjectInvocationHandler.java:179)
        at com.sun.proxy.$Proxy0.getInformation(Unknown Source)
        at me.WaffleMast3r.HCore.HCoreServer.Info.DedicatedServersInfo.update(DedicatedServersInfo.java:98)
        at me.WaffleMast3r.HCore.HCoreServer.Info.DedicatedServersInfo$1.run(DedicatedServersInfo.java:43)
        at java.util.TimerThread.mainLoop(Timer.java:555)
        at java.util.TimerThread.run(Timer.java:505)

0 个答案:

没有答案