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

时间:2021-08-14 15:51:03

Server:

Registry registry = LocateRegistry.createRegistry(1099);

InventoryInterface Inventory = new Inventory(registry);

registry.bind("Inventory", Inventory);

Client:

Registry registry = LocateRegistry.getRegistry(1099);


InventoryInterface inventory = (InventoryInterface) registry.lookup("Inventory");


String product_id = inventory.newProduct();

ProductFacade product_1 = (ProductFacade) registry.lookup(product_id);

The Problem is the exception happens at the casting, in this case it happens at: 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

2 个解决方案

#1


1  

Whatever is bound to the Registry under the name you're looking up does not implement the rmi.ProductFacade remote interface.

无论以您所查找的名称绑定到注册表,都不会实现rmi.ProductFacade远程接口。

So I'm wondering if i should for example restart the Registry before casting again

所以我想知道我是否应该重新启动注册表再次投射之前

Certainly not. (a) You can't restart it from the client, and (b) all you would get would be an empty Registry. The suggestion doesn't make sense.

当然不是。 (a)您无法从客户端重新启动它,并且(b)您将获得的只是一个空的注册表。这个建议没有意义。

Hard to see why InventoryInterface.newProduct() returns a String instead of the actual new ProductFacade object. Also why listAllProducts() returns a String rather than a String[]. I would redesign this without such heavy use of the Registry as follows:

很难理解为什么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;
}

#2


0  

It can be about how you bind it. For example, if ProductFacade implements InventoryInterface you might need to cast it as InventoryInterface instead of ProductFacade.

它可以是关于你如何绑定它。例如,如果ProductFacade实现InventoryInterface,则可能需要将其强制转换为InventoryInterface而不是ProductFacade。

#1


1  

Whatever is bound to the Registry under the name you're looking up does not implement the rmi.ProductFacade remote interface.

无论以您所查找的名称绑定到注册表,都不会实现rmi.ProductFacade远程接口。

So I'm wondering if i should for example restart the Registry before casting again

所以我想知道我是否应该重新启动注册表再次投射之前

Certainly not. (a) You can't restart it from the client, and (b) all you would get would be an empty Registry. The suggestion doesn't make sense.

当然不是。 (a)您无法从客户端重新启动它,并且(b)您将获得的只是一个空的注册表。这个建议没有意义。

Hard to see why InventoryInterface.newProduct() returns a String instead of the actual new ProductFacade object. Also why listAllProducts() returns a String rather than a String[]. I would redesign this without such heavy use of the Registry as follows:

很难理解为什么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;
}

#2


0  

It can be about how you bind it. For example, if ProductFacade implements InventoryInterface you might need to cast it as InventoryInterface instead of ProductFacade.

它可以是关于你如何绑定它。例如,如果ProductFacade实现InventoryInterface,则可能需要将其强制转换为InventoryInterface而不是ProductFacade。