Results 1 to 2 of 2
  1. #1
    driiper is offline Member
    Join Date
    Apr 2012
    Posts
    20
    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
    20
    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)

Similar Threads

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