File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes ClassCast Exception with RMI 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 "ClassCast Exception with RMI" Watch "ClassCast Exception with RMI" New topic
Author

ClassCast Exception with RMI

Jo Fail
Greenhorn

Joined: Feb 05, 2003
Posts: 18
I am still grappling with sorting out RMI and my lock/unlock. I hope that someone can point me in the right direction. I have seen plenty of posts from people who have had this problem but mine seems ever so slightly different.
I am implementing the RMI factory pattern.
I have two classes server side - RemoteData and RemoteDataFactory both of which extends UnicastRemoteObject and implement interfaces RemoteDataInterface and RemoteDataFactoryInterface respectively - both interfaces extends Remote.
So far so good.
I firstly start my server (the RemoteDataFactory ) which creates itself, starts the registry and registers itself thus
RemoteDataFactory rdf = new RemoteDataFactory();
LocateRegistry.createRegistry(1099);
Naming.rebind("RemoteDataFactory", rdf);
Later my client gets the factory object from the registry and calls it's getdata() method which returns a RemoteData object thus
Registry registry = LocateRegistry.getRegistry(1099);
RemoteDataFactoryInterface rdfi = (RemoteDataFactoryInterface)registry.lookup("RemoteDataFactory");
RemoteDataInterface data = (RemoteDataInterface)rdfi.getRemoteData();
but this last line always gives me a ClassCastException thus
Exception occurred during event dispatching:
java.lang.ClassCastException: suncertify.server.RemoteData_Stub etc
Even if I try and cast the result of my getRemoteData method to a plain old Object I still get the same error.
(I have copied my stubs to both the server and the client package and this doesn't seem to help)
I feel sure that this is all to do with the way I am using the rmiregistry but can't seem to quite get the combination correct.
Please can somebody help - I hope that I haven't made my explanation too confusing!
John Smith
Ranch Hand

Joined: Oct 08, 2001
Posts: 2937

RemoteDataInterface data = (RemoteDataInterface)rdfi.getRemoteData();
but this last line always gives me a ClassCastException thus
Exception occurred during event dispatching:
java.lang.ClassCastException: suncertify.server.RemoteData_Stub etc

How does your getRemoteData() method look like?
Jo Fail
Greenhorn

Joined: Feb 05, 2003
Posts: 18
It looks like this -
public RemoteData getRemoteData() throws RemoteException, IOException{
try{remoteData = new RemoteData(FILE_NAME);
}
catch(IOException ioe){
System.out.println("Unable to open specified file, please check path name and try again");
throw ioe;
}
return remoteData;
}
where variables are defined as
private RemoteData remoteData;
private static String FILE_NAME;
[ April 22, 2003: Message edited by: Jo Fail ]
Bigwood Liu
Ranch Hand

Joined: Feb 26, 2003
Posts: 240
I think it is because what you bind at server side don't conform with RemoteDataFactoryInterface:
Jo Fail
Greenhorn

Joined: Feb 05, 2003
Posts: 18
I'm sorry I'm not quite sure what you mean Damu - could you elaborate?
Jo Fail
Greenhorn

Joined: Feb 05, 2003
Posts: 18
I have finally sorted it! I'm very happy. I'm not sure if it's what you meant Damu but thanks for your help.
I found an old posting. ClassCastException with RMI 3rd March 2002 by Derek Drever. Now I know why Eugene asked me what my getRemoteData() method looked like.
Basically my getRemoteData() method needed to return a RemoteDataInterface rather than it's implementation RemoteData.
All these interfaces and implementations don't half confuse me!!!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: ClassCast Exception with RMI