简体   繁体   English

Java RMI类强制转换异常

[英]Java RMI Class Cast Exception

My project gets class Cast Exception when I started server and then I tried to add customer using add customer UI but when I tried for it it returns a Class Cast Exception from the server connector class. 当我启动服务器时,我的项目获得类Cast Exception,然后我尝试使用添加客户UI添加客户,但是当我尝试使用它时,它从服务器连接器类返回Class Cast Exception。

interface CustomerController 接口CustomerController

    public interface CustomerController {
    public boolean addCustomer(Customer customer)throws RemoteException,IOException,ClassNotFoundException;
}

ServerStart,java ServerStart,JAVA

    public class ServerStart {
    public static void main(String[] args) {
          try {
            Registry registry=LocateRegistry.createRegistry(5050);
            System.out.println("Server is starting..");
            registry.rebind("Server", new CustomerControllerImpl());
        } catch (RemoteException ex) {
            Logger.getLogger(ServerStart.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}

ServerConnector.java ServerConnector.java

    public class ServerConnector {

    private static ServerConnector serverConnector;
    private CustomerController customerController;

    private ServerConnector() throws NotBoundException, MalformedURLException, RemoteException {
        customerController = (CustomerController) Naming.lookup("rmi://localhost:5050/Server");
    }

    public static ServerConnector getServerConnector() throws NotBoundException, MalformedURLException, RemoteException {
        if (serverConnector == null) {
            serverConnector = new ServerConnector();
        }
        return serverConnector;
    }

    public CustomerController getCustomerController() {
        return customerController;
    }
}

Class cast Exception occurs at ServerConnector.java file at 类强制转换异常发生在ServerConnector.java文件处

customerController = (CustomerController) Naming.lookup("rmi://localhost:5050/Server");

CustomerControllerImpl.java CustomerControllerImpl.java

    public class CustomerControllerImpl extends UnicastRemoteObject implements CustomerController{

    private final CustomerFileAccess customerFileAccess = new CustomerFileAccess();

    public CustomerControllerImpl() throws RemoteException{

    }

    @Override
    public boolean addCustomer(Customer customer) throws RemoteException, IOException, ClassNotFoundException {
        return customerFileAccess.addCustomer(customer);
    }
}

here I attached the netbeans project which can be download thourgh this link 在这里我附上了netbeans项目,可以通过这个链接下载

Thank you!. 谢谢!。

浏览文档 ,我相信这可能是因为您的界面没有扩展java.rmi.Remote。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM