File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

ClassCast Exception with RMI

 
Jo Fail
Greenhorn
Posts: 18
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2937
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 18
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 240
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think it is because what you bind at server side don't conform with RemoteDataFactoryInterface:
 
Jo Fail
Greenhorn
Posts: 18
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm sorry I'm not quite sure what you mean Damu - could you elaborate?
 
Jo Fail
Greenhorn
Posts: 18
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!!!
 
Consider Paul's rocket mass heater.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic