ClassCastException in RMI application

Discussion in 'Java' started by atilagunes@gmail.com, May 2, 2007.

  1. Guest

    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(){

    }


    }
     
    , May 2, 2007
    #1
    1. Advertising

  2. Guest

    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..
     
    , May 2, 2007
    #2
    1. Advertising

  3. Esmond Pitt Guest

    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.
     
    Esmond Pitt, May 3, 2007
    #3
  4. Guest

    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 ?

    On 3 MayĆ½s, 09:44, Esmond Pitt <> wrote:
    > 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.
     
    , May 3, 2007
    #4
  5. Esmond Pitt Guest

    wrote:
    > 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.
     
    Esmond Pitt, May 4, 2007
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Luther Baker

    ClassCastException and ClassLoaders

    Luther Baker, Sep 9, 2003, in forum: Java
    Replies:
    3
    Views:
    936
    Luther Baker
    Sep 10, 2003
  2. Richard A. DeVenezia
    Replies:
    2
    Views:
    626
    Andrew Thompson
    Jan 8, 2004
  3. Buu Nguyen

    RMI, JINI or RMI/IIOP

    Buu Nguyen, Aug 25, 2004, in forum: Java
    Replies:
    1
    Views:
    593
    Sudsy
    Aug 25, 2004
  4. JScoobyCed

    RMI Vs RMI over IIOP

    JScoobyCed, Jan 28, 2005, in forum: Java
    Replies:
    1
    Views:
    767
    Dag Sunde
    Jan 28, 2005
  5. Jan Schulze
    Replies:
    1
    Views:
    593
    Esmond Pitt
    Mar 26, 2005
Loading...

Share This Page