java.lang.ClassCastException : com. sun.proxy.$Proxy1 无法转换为

标签 java rmi rmiregistry

服务器:

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/

相关文章:

java - JUnit 和 RMI 应用程序的最佳实践,RMI 注册表

java - 将输入读取到每行具有不同列数的 int 2D 数组中

java - 检查按钮周围是否有空白区域

java - 远程 JVM 机器人调用

Java RMI - 检查注册表是否存在

java - 使用 ProcessBuilder 时客户端无法连接 RMI 注册表

java - Swing 持久弹出窗口

java - httpUrlconnection 向任何调用返回 500 错误代码

java - Weblogic EJB 调用在中等负载下开始失败并出现OptionalDataException