aspose file tools*
The moose likes Distributed Java and the fly likes Stub cannot be cast to Interface Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Distributed Java
Bookmark "Stub cannot be cast to Interface" Watch "Stub cannot be cast to Interface" New topic
Author

Stub cannot be cast to Interface

Georg Brunmayr
Greenhorn

Joined: Feb 19, 2010
Posts: 7
Hey...
Sorry for my bad english, i'm austrian... ;)
I have the following problems with a little Program, which should translate Methods from the ACL- Management-Classes over an RMI-Adapter to the client:
I created on server-side a RMI-Adapter- Class + Interface. I copied the Interface from server to client (they are in different packages,
because I thought, it's easier to split them up in several projects, if I seperate them in packages in advance).
In case of an import of the Interface to the client, there are no problems, but when i copy the file from server to client, I get the following error on execution:

java.lang.ClassCastException: server.RMIAdapter_Stub cannot be cast to client.RMIAdapterIF
at client.ACLGui.<init>(ACLGui.java:63)
at client.Main.main(Main.java:11)


the relevant client code:

My class-hierachy:

#client
---ACLGui.class
---Main.java
---RMIAdapterIF.class
#server
---RMIAdapter.class
---RMIAdapterIF.class


My RMIAdapter.class:


I hope, my requests weren't too unintelligible and you could help me before despair ;)
thanks a lot
George
Suhas Bilaye
Ranch Hand

Joined: Sep 10, 2009
Posts: 80
Does your RMIAdapterIF interface extend from java.rmi.Remote?

Thanks and Regards,
Suhas
http://www.xplore-java.blogspot.com/
Georg Brunmayr
Greenhorn

Joined: Feb 19, 2010
Posts: 7
yes it does...
Suhas Bilaye
Ranch Hand

Joined: Sep 10, 2009
Posts: 80
Well I am not very sure whether this will work.
But you may try this,


Add this constructor to your RMIAdapter class. Actually UnicastRemoteObject has a protected constructor which throws a RemoteException which you have not considered try doing it.
Georg Brunmayr
Greenhorn

Joined: Feb 19, 2010
Posts: 7
Oh, i took into consideration that the Constructor has to throw an RemoteException, but the problem still exists...
Here is my full RMI Adapter:
Suhas Bilaye
Ranch Hand

Joined: Sep 10, 2009
Posts: 80
Usually this type of exception occurs when the stub class which you have generated does not implement the interface to which you are casting the Remote stub.
Have you renamed the interface RMIAdapterIF after you have generated the stub? If yes try generating the stub class again using the rmic compiler command again.
Georg Brunmayr
Greenhorn

Joined: Feb 19, 2010
Posts: 7
no, I generated the stub always new, when i compiled my files... and i didn't rename anything...
My Adapter implements the interface, so the stub also has to implement the same interface, isn't?
If I import the Interface from the other package, there's no problem... But that's a little bit unportable ;)
So i seperated them, to use the packages completely independent from each other...

...
my Interface:

Suhas Bilaye
Ranch Hand

Joined: Sep 10, 2009
Posts: 80

java.lang.ClassCastException: server.RMIAdapter_Stub cannot be cast to client.RMIAdapterIF
at client.ACLGui.<init>(ACLGui.java:63)
at client.Main.main(Main.java:11)


As per your first post, your stub class is in server while your RMIAdapterIF is in client package right?

In the last post, you have your interface RMIAdapterIF in server package, well I think there is some inconsistency, the same interface is to be shared between the client and the server because your generated stub class has implemented the RMIAdapterIF from server package while you are trying to cast the stub to RMIAdapterIF from client package.
Georg Brunmayr
Greenhorn

Joined: Feb 19, 2010
Posts: 7
ok i already thougt about that but whats the solution for such a problem?
when i copy the interface without changing the package declaration, i get an error, or not??
Suhas Bilaye
Ranch Hand

Joined: Sep 10, 2009
Posts: 80
Of course you cannot copy the interface to some other package without changing the package declaration.
So what you need to do is to have a shared package between the server and the client which will share this interface.
Even if you have the signature, methods and everything same the package declaration is different so there are 2 separate .class files for the interface with 2 different package declarations so you simply cannot cast it and make it work that way..
You can export the interface in a jar to the client but still without changing the package declaration
Georg Brunmayr
Greenhorn

Joined: Feb 19, 2010
Posts: 7
yeah good idea, it works ;)

thank you...
 
Don't get me started about those stupid light bulbs.
 
subject: Stub cannot be cast to Interface