gpt4 book ai didi

Java RMI 类转换异常

转载 作者:搜寻专家 更新时间:2023-11-01 02:41:23 26 4
gpt4 key购买 nike

当我启动服务器时,我的项目获得类 Cast Exception,然后我尝试使用添加客户 UI 添加客户,但是当我尝试它时,它从服务器连接器类返回一个类 Cast Exception。

接口(interface)客户 Controller

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

服务器启动,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);
}
}
}

服务器连接器.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;
}
}

ServerConnector.java 文件发生类转换异常

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

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);
}
}

我在这里附上了 netbeans project which can be download thourgh this link

谢谢!

最佳答案

浏览 docs ,我相信这可能是因为您的接口(interface)没有扩展 java.rmi.Remote。

关于Java RMI 类转换异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32459128/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com