Re: java.lang.InstantiationException:$Proxy0
Peter Jones <[email protected]> Tue, 10 Feb 2009 13:28:53 -0500
| Newsgroups | gmane.comp.java.sun.rmi |
|---|---|
| Message-ID | <20090210182853.GA22016@east> |
On Mon, Feb 09, 2009 at 06:39:47AM -0800, lachhman wrote: > Actually the problem is that I have to send the stub from one object(sender) > to another object(receiver), and this can easily be done by calling the > method of receiver which carries stub as an argument. However, the > environment I working under does not allow any object to have a method to > carry an argument of remote object. However, methods can carry arguments of > basic data types( String, float etc.) and Class instance of any class type. > > So, what I am doing is that I am creating a class instance of obtained stub > in sender class and sending this as an argument to the method of receiver. > In the body of method I am re-constructing the stub from this Class > instance(e.g., Xyz stub = (Xyz) classInstance.newInstance()) , This throws > an excpetion(Java.lang.InstantiationExceptio:$Proxy0). The basic reason for the InstantiationException, as explained in its API doc, is that you are invoking Class.newInstance on a class that does not have a no-arg constructor. The class in question, a dynamic proxy class, indeed does not have a no-arg constructor-- rather, dynamic proxy classes only have a one-arg constructor, which takes the InvocationHandler for the proxy instance. In order to reflectively instantiate a class using a constructor with arguments, you need to get the desired Constructor object from the Class object and then invoke Constructor.newInstance with the arguments. But more generally, you can't reconstruct an RMI stub from its class alone-- there is essential instance state, like the host, port, and object identifier of the remote object. The only really portable way of capturing and recreating that instance state is through object serialization/deserialization. Can the "receiver" do the registry lookup itself? Or can you pass it a byte[] containing the serialized form of the RMI stub? -- Peter =========================================================================== To unsubscribe, send email to [email protected] and include in the body of the message "signoff RMI-USERS". For general help, send email to [email protected] and include in the body of the message "help". For a list of frequently asked RMI questions please refer to: http://java.sun.com/j2se/1.3/docs/guide/rmi/faq.html To view past RMI-USERS postings, please see: http://archives.java.sun.com/archives/rmi-users.html