当我启动服务器时,我的项目获得类Cast Exception,然后我尝试使用添加客户UI添加客户,但是当我尝试使用它时,它会从服务器连接器类返回Class Cast Exception。
接口CustomerController
public interface CustomerController {
public boolean addCustomer(Customer customer)throws RemoteException,IOException,ClassNotFoundException;
}
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
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
谢谢!。