ClassCastException:Proxy0无法强制转换为interfaces.Patient

时间:2014-06-24 09:02:18

标签: java error-handling casting network-programming rmi

嗨,我一直收到此错误,不知道为什么

java.lang.ClassCastException: com.sun.proxy.$Proxy0 cannot be cast to interfaces.Patient
    at main.RemoteObjects.<init>(RemoteObjects.java:15)
    at DRV_GUI.<init>(DRV_GUI.java:49)
    at Main.main(Main.java:13)

我有一个这样的服务器应用程序:

import java.rmi.Naming;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;

public class MainServer {

PatientImpl patient = null;
LoginImpl login = null;

public MainServer(String[] args) {
    // TODO constructor
    System.out.println("Ich bin der Konstruktor von Main.java");
}

public static void main(String[] args) {
    MainServer server = new MainServer(args);
    server.start();
}

private void start() {
    try {
        patient = new PatientImpl();
        login = new LoginImpl();
        Registry registry = LocateRegistry.createRegistry(1099);
        Naming.rebind("patient", patient);
        Naming.rebind("login", login);
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}
}

患者界面如下:

package interfaces;

import java.rmi.RemoteException;
import java.util.LinkedHashMap;

public interface Patient {
    public LinkedHashMap<String, LinkedHashMap<Integer, LinkedHashMap<String, Object>>> patientSuchen(String id, User user) throws RemoteException;
    public boolean kostenzusageSpeichern (String id, User user) throws RemoteException;
    public boolean aufnahmeStornieren (String id, User user) throws RemoteException;
    public boolean aufnahmeAendern (String id, User user) throws RemoteException;
    public boolean entlassungStornieren (String id, User user) throws RemoteException;
    public boolean entlassungAendern (String id, User user) throws RemoteException;
    public boolean eingangEntlassungsStatistikStorieren (String id, User user) throws RemoteException;
    public boolean eingangEntlassungsStatistikAendern (String id, User user) throws RemoteException;
    public LinkedHashMap<String, LinkedHashMap<String, Object>> getVeraengerunglUnterbrechung(String id, String table, User user) throws RemoteException;
    public boolean setVeraengerunglUnterbrechung(String id, String table, User user) throws RemoteException;
}

在客户端和服务器端也一样,包名也是一样的;

这里是RemoteObjects.java代码:

    package main;

    import interfaces.*;
    import java.rmi.Naming;

    public class RemoteObjects {
        Login login;
        public Patient patient;
        public String remoteIP = "127.0.0.1";

        /*
         * secrets[0] = PatientI
         */
        public RemoteObjects() {
        try {
            String name = "rmi://" + remoteIP + ":" + 1099 + "/";
            patient = (Patient) Naming.lookup(name + "patient");
            login = (Login) Naming.lookup(name + "login");
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        }

    }
我做错了什么?如果有人有想法会很高兴

1 个答案:

答案 0 :(得分:2)

您的Patient界面应扩展接口java.rmi.Remote,使其在远程端可用。