• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

ClassCastException with RMI

 
Derek Drever
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I have recently coded the server side of my project and constantly recieve a ClassCastException when trying to retrieve a connection to the remote database. The exact sequence is this:
1. I have all the code for server and client in one directory to facilitate testing. I have set the CLASSPATH to this working directory and everything is in the default package.
2. I have run rmic on two classes: Connection and RemoteData
3. I bind a Connection object on the server and successfully perform a lookup on the client. The connection object has one method getDB()
4. I call the getDB method to return a RemoteData object and get a ClassCastException with the following stack trace:
java.lang.ClassCastException: RemoteData_Stub
at Connection_Stub.getDB(Unknown Source)
at DBFactory.getDBAdaptor(DBFactory.java:18)
at FBNClient.main(FBNClient.java:26)
The RemoteData object implements Remote and extends UnicastRemoteObject.
I am at a bit of a loss as to what the problem is and would appreciate any insightful suggestions.
Regards,
Derek.
 
Rene Larsen
Ranch Hand
Posts: 1179
Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
I think you need to cast the remote object you recive from the server like this:

Hope this helps...
/Ren´┐Ż
[ March 03, 2002: Message edited by: Rene Larsen ]
[ March 03, 2002: Message edited by: Rene Larsen ]
 
Derek Drever
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am casting to the Interface type, here is simplification of a small test program, which details pretty much what I am trying to do, prize to whoever spots the deliberate mistake:
public interface Connect extends Remote
{
public RemoteData getData() throws RemoteException;
}
public class Connection extends UnicastRemoteObject implements Connect
{
public Connection() throws RemoteException
{
}
public RemoteData getData() throws RemoteException
{
return new RemoteData();
}
}
public interface DataAccess extends Remote
{
public void print() throws RemoteException;
}
public class RemoteData implements DataAccess
{
public RemoteData() throws RemoteException
{
UnicastRemoteObject.exportObject(this);
}
public void print() throws RemoteException
{
System.out.println("Hello World");
}
}
public class Server
{
public static void main(String args[])
{
...
registry.rebind("Connection", con);
...
}
}
public class Client
{
public static void main(String[] args)
{
String url = "rmi://localhost/";
try
{
Connect con = (Connect)Naming.lookup(url + "Connection");
DataAccess data = (DataAccess)con.getData();
data.print();
}
catch(Exception e)
{
System.out.println(e);
}
}
}
 
Karthik Guru
Ranch Hand
Posts: 1209
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok you are saying the look up went through fine but the cast fails.
could you try printing the classname after doing a look up. It s'd be same as the stub class name.
Object o = registry.lookup("something");
System.out.println(o.getClass.getName());
this should hopefully tell you something :-)
 
Derek Drever
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The registry lookup works fine and returns the correct class, it the call to the connection objects getData method that fails, feel free to run the test programs and see if they work on your machine, I may have a problem with my environment or java installation.
 
Karthik Guru
Ranch Hand
Posts: 1209
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Allright,
change the Connect interface to return a DataAccess instance rather than the implementation :-)
public interface Connect extends Remote
{
public DataAccess getData() throws RemoteException;
}
I also found that you are doing
UnicastRemoteObject.export().
I think just doing that is not enough. You might have to provide implementation for other methods as well (like say equals(), hashCode() etc)
Unicast does lots of things for us i guess. So try avoiding it unless you are forced to.
Better extend the class!
Originally posted by Derek Drever:
prize to whoever spots the deliberate mistake:

So what do i get for this now ?? :-)
 
Derek Drever
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Karthik,
Damn, I was convinced I had tried setting the return type to the interface type when trying to find the solution, sometimes you can't see the wood for the trees, anyway thank you very much for your help, as for the prize well it's ah hmmm lets see it's, well it's in the post....
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic