• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Stub cannot be cast to Interface

 
Georg Brunmayr
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does your RMIAdapterIF interface extend from java.rmi.Remote?
 
Georg Brunmayr
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes it does...
 
Suhas Bilaye
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yeah good idea, it works ;)

thank you...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic