aspose file tools*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes Rmi lookup Class Cast Exception Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "Rmi lookup Class Cast Exception" Watch "Rmi lookup Class Cast Exception" New topic
Author

Rmi lookup Class Cast Exception

Sri Addanki
Ranch Hand

Joined: Apr 27, 2001
Posts: 195
Hi,
When i do Naming.lookup on the RmiServerFactory,(which is bound to registry) i think its not finding the stub, because it gives ClassCast Exception.
But when i do Naming.lookup as rmi://localhost/ on the same RmiServerFactory class, then its working.
Can someone please help me with this ClassCast Exception ?
My RmiServerFactory class extends UnicastRemoteObject and implements RemoteInterfaceFactory
thanks,
sri
Jawad Kakar
Ranch Hand

Joined: Oct 06, 2002
Posts: 82
You have to cast Naming.lookup to the interface which extends Remote.
i.e

and the lookup part looks like

Jawad
Sri Addanki
Ranch Hand

Joined: Apr 27, 2001
Posts: 195
Hi Jawad,
I did the same way.
My application runs well when i use,
"... Naming.lookup as rmi://localhost/ on the same RmiServerFactory class.."
But if i just say

then i get ClassCastException.
I feel the stub is not found by the client. but why?
can someone help...
thanks,
sri
[ December 31, 2002: Message edited by: Sri Addanki ]
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17260
    
    6

you are including your stub in with the client.jar file right?
Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Sri Addanki
Ranch Hand

Joined: Apr 27, 2001
Posts: 195
Originally posted by Mark Spritzler:
you are including your stub in with the client.jar file right?
Mark

Oh, no Mark!
Thanks for that tip.
Thanks again,
Yes i think i need to either include the stub or use codebase, which i'm not doing.
Also one another question, earlier before using factory in RMI, i did the same rmi:// lookup for the rmiserver and it was working.
Now, after creating a rmifactory, i now try to rebind rmifactory class instead of rmiserver class. And then i do the same rmi:// lookup for the rmifacctory class, but its not working. Don't know why?
Despite all this, if i do rmi://localhost lookup its working!
Now in my design i have 2 classes extending UnicastRemoteObject and only RMIFactory class getting registered in the registry.
My Code:
Sri Addanki
Ranch Hand

Joined: Apr 27, 2001
Posts: 195
Hi Mark,
Can u please help.....
I get class cast exception when i do rmi://lookup.
And i DONOT get any exception if i do rmi://localhost/ lookup, why? Apparently i feel the stub is not found, by the class. But why?
p.s: I took care of policy file and codebase.
I have 2 classes extending Remote,
creating 2 stubs and 2 skeletons.
But its the factory class that i'm doing lookup for.
I'm back after 3 month vacation and am still stuck with the same problem. Hence i'm posting in the same thread.
thanks for the help,
sri
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17260
    
    6

The ConnectionFactory class and the Remote Implementation of your Remote interface are the two classes that need to be RMIC'd to create stubs.
When you are in the client and looking up a remote object, you need to have a server name in there, that is why localhost works and without it, it doesn't. localhost is the server location, which in this case happens to be the same machine that the client is running on.
If the server is on a nother box, then the localhost should be replaced with the server name or ip address.
Mark
Sri Addanki
Ranch Hand

Joined: Apr 27, 2001
Posts: 195
Hi Mark,
Thanks.
So what is that i'm exactly looking for?
Where do you think the problem is? can you please explain, am i missing something here.

The ConnectionFactory class and the Remote Implementation of your Remote interface are the two classes that need to be RMIC'd to create stubs.

yes, i do have the same.

thanks,
sri
Sri Addanki
Ranch Hand

Joined: Apr 27, 2001
Posts: 195
Hi Mark,
while i was reading
http://forum.java.sun.com/thread.jsp?forum=58&thread=128260
i found this message:
do you think, this could be the reason for my error too.

i also had the same problem (class cast exception, at run time ) when i tried to implement more than one interface and tried to cast the server to one among them. the problem solved when i put all the interface methods to one interface. Are u doing somethging similar to this???
if anybody knows how the server can implement more than one userdefined interface, please advise.
thanks.

thanks,
sri
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17260
    
    6

No, actually you had already solved it when you said that it works with rmi://localhost/lookup.
You have to have the server address in your lookup. in "rmi://lookup", there is no server address and that is why it fails.
Mark
Sri Addanki
Ranch Hand

Joined: Apr 27, 2001
Posts: 195
Thanks for the reply, Mark
Can you please look at this code:

RemoteIDataFactory is the interface for RMIDataServerFactory
RemoteIData is also a interface for creating new RMIDataServer.
[ March 30, 2003: Message edited by: Sri Addanki ]
Sri Addanki
Ranch Hand

Joined: Apr 27, 2001
Posts: 195
Originally posted by Mark Spritzler:
You have to have the server address in your lookup. in "rmi://lookup", there is no server address and that is why it fails.
Mark

Hey, what do you mean by server address, can you please look into my code, posted above and tell me, where i'm going wrong?
thanks,
sri
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17260
    
    6

Is that method in your client. Because there aren't any lookups in the server classes. So the RMIDataServerFactory is the only class in the RMI Registry, and that is looked up on the client side, so to speak.
Here is code from my DataAccessFactory, it gets the connection from the "RMIDataServerFactory"

and here is the code im my ConnectionFactory that returns a DataAccessRemote class.

Hope that helps
Mark
Sri Addanki
Ranch Hand

Joined: Apr 27, 2001
Posts: 195

String url = "rmi://" + host+ "/connectionFactory";


By saying host here, do you mean the name of the remote server, you are connecting to.
If that is so, even i have that in my string url. (please see my code above.)
can you please help me.
thanks, sri
[ March 31, 2003: Message edited by: Sri Addanki ]
Sri Addanki
Ranch Hand

Joined: Apr 27, 2001
Posts: 195
Originally posted by Mark Spritzler:
Is that method in your client. Because there aren't any lookups in the server classes. So the RMIDataServerFactory is the only class in the RMI Registry, and that is looked up on the client side, so to speak.

Yes, createRemoteConnection() is a method in my client. And the only class registered in registry is RMIDataServerFactory, which is lookedup by the client through this method.
Sri Addanki
Ranch Hand

Joined: Apr 27, 2001
Posts: 195
Hi Mark,
By the way is this the right way to print the stub:

Because i'm getting something like this as output when i use the above code. Why doesn't this code print the actual name of the stub?


Is there a way to print out the actual name of the stub, that the client is looking for in the registry.
thanks,
sri
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17260
    
    6

When you do your lookup are you casting the class to the interface. like in the following

see how I cast the class returned from the lookup to a "ConnectionFactory" class this is the interface that the stub implements, and I can use it.
Mark
Sri Addanki
Ranch Hand

Joined: Apr 27, 2001
Posts: 195
Yes Mark, i'm casting this to interface. And here at this very particular statement i get the class cast exception.
Here's my code:

RemoteIDataFactory is the interface for RMIDataServerFactory.
Mark, can you please clarify this:
My Naming.lookup is:

and Your Naming.lookup is:
quote:
--------------------------------------------------------------------------------

String url = "rmi://" + host+ "/connectionFactory";
--------------------------------------------------------------------------------

By saying host here, do you mean the name of the remote server, you are connecting to.
(please see my code posted earlier.)
and in RMIDataServerFactory class, which is bound to registry, i have this code:

Here, args[0] is the remote server name, given as a command line argument.
can you please help,
thanks,
sri
[ April 01, 2003: Message edited by: Sri Addanki ]
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17260
    
    6

Yes, host is the server name or ip address.
so the string could look like
host = "localhost"
"rmi://localhost/connectionFactory"
if host = "192.255.255.255"
then
"rmi://192.255.255.255/connectionFactory"
is the resulting url string value.
"Here, args[0] is the remote server name, given as a command line argument."
Again my rebind does not have any server name in it at all, because it is code within my server, so why do I need to give is a server anme or ip address.
my rebind call passes (object name, object)
so I have
rebind("connectionFactory", connectionFactory)
does that help?
Mark
Sri Addanki
Ranch Hand

Joined: Apr 27, 2001
Posts: 195
Hi Mark,
Thanks for that response. I'm trying to understand a few things here.
In Naming.lookup("rmi://"+host+"serverName");
What i understand now, is that you have hard coded the RemoteServerName, in the
Naming.rebind() method.
But how did you manage the
remotehostname/remotecomputername/ipaddress?
Did you ask the client,user to type in that info? How did you get the host value in your string url?
thanks,
sri
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17260
    
    6

Yes, the user when they start the client in remote mode type
java -jar client.jar 255.255.255.255
or
java -jar client.jar localhost

then in local mode, they only had to type
java -jar client.jar
Mark
Sri Addanki
Ranch Hand

Joined: Apr 27, 2001
Posts: 195
WOW Mark, thanks a lot!!!
Atlast i got it!
Thanks to your patience.
sri
Sri Addanki
Ranch Hand

Joined: Apr 27, 2001
Posts: 195
Now, Mark, while coding, should i consider this case too...
If the client doesn't mention anything about localhost, or hostname or ipaddress then by default the code will be:

Naming.lookup("rmi://"+remoteObject);

then it should automatically do lookup like this, right?

Naming.lookup("rmi://"+localhost/+remoteObject);

If so, then i'm still stuck with the same old problem.
I get classcast exception, only when i do lookup as rmi://remoteObject
and i donot get classcast exception if i say rmi://localhost/remoteobject or rmi://hostname/remoteobject
Can you please help me,
thanks,
sri
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17260
    
    6

"If the client doesn't mention anything about localhost, or hostname or ipaddress "
Then I would assume that it would be in local mode, which means there won't be any server or RMI.
Mark
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Rmi lookup Class Cast Exception