服务器:
Registry registry = LocateRegistry.createRegistry(1099);
InventoryInterface Inventory = new Inventory(registry);
registry.bind("Inventory", Inventory);
客户:
Registry registry = LocateRegistry.getRegistry(1099);
InventoryInterface inventory = (InventoryInterface) registry.lookup("Inventory");
String product_id = inventory.newProduct();
ProductFacade product_1 = (ProductFacade) registry.lookup(product_id);
问题是异常发生在转换时,在本例中发生在:ProductFacade product_1 = (ProductFacade)registry.lookup(product_id);
异常(exception):
Exception in thread "main" java.lang.ClassCastException: com.sun.proxy.$Proxy2 cannot be cast to rmi.ProductFacade
最佳答案
以您正在查找的名称绑定(bind)到注册表的任何内容都不会实现rmi.ProductFacade
远程接口(interface)。
So I'm wondering if i should for example restart the Registry before casting again
当然不是。 (a) 您无法从客户端重新启动它,并且 (b) 您将得到的只是一个空的注册表。这个建议没有意义。
很难理解为什么 InventoryInterface.newProduct()
返回一个 String
而不是实际的新 ProductFacade
对象。另外为什么 listAllProducts()
返回 String
而不是 String[]
。我会在不大量使用注册表的情况下重新设计它,如下所示:
public interface InventoryInterface extends Remote {
public ProductFacade newProduct() throws RemoteException;
public ProductFacade getProduct(String id) throws RemoteException;
public String[] listAllProducts() throws RemoteException;
}
关于java.lang.ClassCastException : com. sun.proxy.$Proxy1 无法转换为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41082941/