ClassCastException in RMI application

A

atilagunes

Hello,

i found some mails complaining about ClassCast Exception but mine is a
bit different and i coulnt find any explanation.. Please help me..


********** CList Interface *************
import java.rmi.*;
import java.util.Properties;

public interface CList extends Remote{
public Properties getClients() throws RemoteException;
}



********** CListImpl ******************
import java.rmi.*;
import java.rmi.server.*;
import java.util.Properties;

public class CListImpl extends UnicastRemoteObject
implements CList {
public CListImpl () throws RemoteException {
}

public Properties getClients() throws RemoteException{
return clients;
}
public Properties clients=new Properties(); // I'm trying to return
back a Properties file..
}


*********** CListClient ****************
import java.rmi.*;
import java.rmi.server.*;
import javax.naming.*;
import java.util.Properties;

public class CListClient {
public static void main(String[] args)
{
System.setProperty("java.security.policy", "client.policy");
System.setSecurityManager(new RMISecurityManager());
String url = "rmi://localhost/";

try
{

Context namingContext = new InitialContext();
CList c1 = (CList) namingContext.lookup(url +
"akclients"); // Register name
Properties clients=new Properties();
clients=(Properties)c1.getClients();
System.out.println(clients);

}
catch (Exception e)
{
e.printStackTrace();
}
}
}


************** In the Server side *******************
import java.util.Properties;
import java.rmi.*;
import java.rmi.server.*;
import javax.naming.*;

public class Server {
public CListImpl c1;
public Server(){

}


}
 
A

atilagunes

sorry , i send wrongly before complate the mail..
i'm going on..

************** In the Server side *******************
import java.util.Properties;
import java.rmi.*;
import java.rmi.server.*;
import javax.naming.*;

public class Server {

public CListImpl c1;

public Server(){

try{
c1 = new CListImpl ();
Context namingContext = new InitialContext ();
namingContext.rebind ("rmi:akclients", c1);
}
catch(Exception e){
e.printStackTrace();
}

}
}


server sides works fine.. but client side gives an error at
CListClient.java when it executes "" CList c1 = (CList)
namingContext.lookup(url + "akclients"); "" line..
it gives java.lang.ClassCastException: CListImpStub cannot be cast to
CList at CListClient.main<CListClient.java:17> that i wrote above what
the line is.

CListImpl uses Properties but CList interface cant cast it.. How can i
send an object ? Please help mee, i need to use this in my project and
i'm not familiar with RMI actually..
 
E

Esmond Pitt

I suspect you have the CList interface in two packages, so the CList
that is implemented by CListImplStub is different from the CList the
client is using. You can't do that. You have to use the *same* remote
interface class, not a similar one with the same name in a different
package.
 
A

atilagunes

Yes, they are in different package.. Coz server side project and
client side projects are in different project, thats why they are in
different package.. but the code which are implemented in CList
interface and CListImpl are same as codes... According to your mail,
i should use same package name for both sides, hmmm, or can i copy
the stub to another project as compiled as class file ?
 
E

Esmond Pitt

Yes, they are in different package.. Coz server side project and
client side projects are in different project, thats why they are in
different package.. but the code which are implemented in CList
interface and CListImpl are same as codes... According to your mail,
i should use same package name for both sides, hmmm,

I didn't say that at all. I said you had to use the same remote
interface class at both sides and that includes not changing its package
name. How you achieve sharing of that single class between multiple
projects is up to you. That doesn't imply that both entire projects
should be in the same package.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top