Results 1 to 6 of 6
  1. #1
    driiper is offline Member
    Join Date
    Apr 2012
    Posts
    25
    Rep Power
    0

    Default RMI - ClassCastException - Proxy cannot be cast

    Hello, i'm currently switching to RMI for networking in my application, i have followed some tutorials, read some on Oracles RMI pages and i belive i understand the concept behind it.

    I found a example on the net and its working with transmitting Strings. however when i try to send my own object it get the error:
    Java Code:
    HelloClient exception: java.lang.ClassCastException: sun.proxy.$Proxy0 cannot be cast to saqib.rasul.server.Fag
    java.lang.ClassCastException: sun.proxy.$Proxy0 cannot be cast to saqib.rasul.server.Fag
    	at sun.proxy.$Proxy0.returnFag(Unknown Source)
    	at saqib.rasul.server.dd.main(dd.java:19)
    Googling for this error tells me from what i can understand that i have not implemented the interface in my class. But i have (Fag implements FagInterface).

    So yeh, i have no idea why i get this error, and what it means.

    Client.java
    Java Code:
    package saqib.rasul.server;
    
    import java.rmi.Naming;
    
    
    
    public class Client {
    	  /**
    	   * Client program for the "Hello, world!" example.
    	   * @param argv The command line arguments which are ignored.
    	   */
    	  public static void main (String[] argv) {
    	    try {
    	   //   HelloInterface hello = (HelloInterface) Naming.lookup ("//localhost/Hello");
    	      FagInterface fag = (FagInterface) Naming.lookup("//localhost/Fag");
    
    	      
    	     // System.out.println (hello.say());
    	      System.out.println(fag.returnFag());
    	    } catch (Exception e) {
    	      System.out.println ("HelloClient exception: " + e);
    	      e.printStackTrace();
    	    }
    	  }
    }
    Breaks on: System.out.println(fag.returnFag());

    Server.java
    Java Code:
    package saqib.rasul.server;
    
    import java.rmi.registry.LocateRegistry;
    import java.rmi.registry.Registry;
    import java.rmi.server.UnicastRemoteObject;
    
    public class Server {
    	/**
    	 * Server program for the "Hello, world!" example.
    	 * @param argv The command line arguments which are ignored.
    	 */
    	public static void main (String[] argv) {
    		try {
    			java.rmi.registry.LocateRegistry.createRegistry(1099);
    			System.out.println("RMI registry ready.");
    			//Naming.rebind ("Hello", new Hello ("Hello, world!"));
               
    			String name = "Fag";
                FagInterface engine = new Fag("Universi");
                FagInterface stub =
                    (FagInterface) UnicastRemoteObject.exportObject(engine, 0);
                Registry registry = LocateRegistry.getRegistry();
                registry.rebind(name, stub);
    			
    			System.out.println ("Hello Server is ready.");
    		} catch (Exception e) {
    			System.out.println ("Hello Server failed: " + e);
    		}
    	}
    }
    FagInterface.java
    Java Code:
    package saqib.rasul.server;
    
    import java.rmi.Remote;
    import java.rmi.RemoteException;
    
    public interface FagInterface extends Remote{
    
    	public Fag returnFag() throws RemoteException;
    	
    	
    }
    Fag.java
    Java Code:
    package saqib.rasul.server;
    
    import java.rmi.RemoteException;
    import java.rmi.server.UnicastRemoteObject;
    import java.util.ArrayList;
    
    /**
     * 
     * @author Per-Arne
     * @description Simple class which contains all subjects (fag) in a single course (kurs). The way this class works is that it takes a ArrayList with subject for a whole week. Then this week is added to another ArrayList which then is the grand total of subjects.
     */
    public class Fag implements FagInterface{
    	private static final long serialVersionUID = 1L;
    	public String fagName;
    	public ArrayList<ArrayList<FagTime>> weeks;
    	private Fag fag;
    
    	public Fag(String fagName) throws RemoteException{
    		this.fag = this;
    	}
    
    	/**
    	 * @param week - Contains a whole week with subjectData, this is added to this course.
    	 */
    	public void addWeek(ArrayList<FagTime> week) throws RemoteException{
    		weeks.add(week);
    	}
    
    	public Fag returnFag() throws RemoteException {
    		return fag;
    	}		
    }
    Thanks
    Driiper

  2. #2
    driiper is offline Member
    Join Date
    Apr 2012
    Posts
    25
    Rep Power
    0

    Default Re: RMI - ClassCastException - Proxy cannot be cast

    Figured it out...

    I had to use the same package name (Which makes sense)

  3. #3
    idelvi is offline Member
    Join Date
    Jun 2013
    Posts
    1
    Rep Power
    0

    Default Re: RMI - ClassCastException - Proxy cannot be cast

    May I know what do you mean by "use same package name"?
    Seems all your package are under package saqib.rasul.server;
    I also have the same problem with you..

  4. #4
    driiper is offline Member
    Join Date
    Apr 2012
    Posts
    25
    Rep Power
    0

    Default Re: RMI - ClassCastException - Proxy cannot be cast

    Both your client and your server needs to have the same package layout.

    Lets say you have a RMI service set up, also you have your server sucessfully set up. in order for the RMI to work, your Server and Client would need to have the same package name on the shared java files.

    If you want to transfer objectX which is in the org.example.client.objectX directory you would have to have this at the same location on both your applications (server/client)


    Hope you get it to work :)


    driiper

  5. #5
    mahab is offline Member
    Join Date
    Aug 2014
    Posts
    2
    Rep Power
    0

    Default Re: RMI - ClassCastException - Proxy cannot be cast

    Quote Originally Posted by driiper View Post
    Both your client and your server needs to have the same package layout.

    Lets say you have a RMI service set up, also you have your server sucessfully set up. in order for the RMI to work, your Server and Client would need to have the same package name on the shared java files.

    If you want to transfer objectX which is in the org.example.client.objectX directory you would have to have this at the same location on both your applications (server/client)


    Hope you get it to work :)


    driiper

    Hi driiper

    Can you elaborate on above please?
    I have a similar problem with the same exception and as far as I'm concerned, all my classes, server-side and client-side programs are in the same package (they were initially in different packages, but after reading this thread I moved them to the same package to see it work), but I still get this error. Below is what I have:

    ObjectTransporter.java
    package uk.ac.rm950.nonPublicDataSerializationServer;

    public interface ObjectTransporter<T> extends java.rmi.Remote {
    public T fetchObject() throws java.rmi.RemoteException;
    }


    Employee.java
    package uk.ac.rm950.nonPublicDataSerializationServer;

    import java.rmi.RemoteException;

    //import uk.ac.rm950.remoteInterface.ObjectTransporter;

    public class Employee implements ObjectTransporter<Employee> {

    public String name;

    public Employee(String name) {
    this.name = name;
    }

    @Override
    public Employee fetchObject() throws RemoteException {
    return this;
    }
    }


    ServerSideProgram.java

    import java.rmi.registry.Registry;
    import java.rmi.server.UnicastRemoteObject;

    //import uk.ac.rm950.remoteInterface.ObjectTransporter;

    public class ServerSideProgram {

    public static void main(String[] args) {
    System.setProperty("java.security.policy", "security/server.policy");

    if (System.getSecurityManager() == null) {
    System.setSecurityManager(new SecurityManager());
    }

    try {
    Registry registry = LocateRegistry.createRegistry(1099);

    ObjectTransporter<Employee> transportingObject = new Employee("Nemo");
    ObjectTransporter<Employee> stub = (ObjectTransporter<Employee>) UnicastRemoteObject.exportObject(transportingObjec t, 0);
    registry.rebind("employee", stub);

    System.out.println("Employee object bound.");
    } catch (RemoteException e) {
    System.out.println("Exception occurred on server side.");
    e.printStackTrace();
    }

    }
    }


    ClientSideProgram.java

    package uk.ac.rm950.nonPublicDataSerializationServer;

    import java.rmi.NotBoundException;
    import java.rmi.RemoteException;
    import java.rmi.registry.LocateRegistry;
    import java.rmi.registry.Registry;

    //import uk.ac.rm950.remoteInterface.ObjectTransporter;

    public class ClientSideProgram {

    public static void main(String[] args) {
    System.setProperty("java.security.policy", "security/server.policy");

    if (System.getSecurityManager() == null) {
    System.setSecurityManager(new SecurityManager());
    }

    try {
    Registry registry = LocateRegistry.getRegistry();
    ObjectTransporter<Employee> transportedObject = (ObjectTransporter<Employee>) registry.lookup("employee");

    // Employee deserializedEmployee = (Employee) transportedObject.fetchObject();
    System.out.println(transportedObject.fetchObject() .name);
    } catch (RemoteException e) {
    e.printStackTrace();
    } catch (NotBoundException e) {
    e.printStackTrace();
    }
    }
    }


    When I run the client program I get this exception:

    Exception in thread "main" java.lang.ClassCastException: com.sun.proxy.$Proxy0 cannot be cast to uk.ac.rm950.nonPublicDataSerializationServer.Emplo yee
    at uk.ac.rm950.nonPublicDataSerializationServer.Clien tSideProgram.main(ClientSideProgram.java:26)

  6. #6
    mahab is offline Member
    Join Date
    Aug 2014
    Posts
    2
    Rep Power
    0

    Default Re: RMI - ClassCastException - Proxy cannot be cast

    I worked what the problem was. In a nutshell, you can't send a remote object to the client (which is why it is called REMOTE object). See my question posted on Stackoverflow and related answers at: java - RMI Exception: proxy cannot be cast to remote object - Stack Overflow. Hope this helps the rest of you :-)

Similar Threads

  1. Replies: 1
    Last Post: 06-19-2012, 03:55 AM
  2. openConnection(Proxy proxy) question
    By Dark in forum New To Java
    Replies: 8
    Last Post: 12-31-2011, 02:15 PM
  3. Replies: 2
    Last Post: 01-21-2011, 07:22 AM
  4. Http - proxy or non-proxy ?
    By Shiv in forum Networking
    Replies: 0
    Last Post: 04-11-2009, 09:07 AM
  5. ClassCastException
    By paulsim in forum Java Applets
    Replies: 2
    Last Post: 08-21-2008, 03:14 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •