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:
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
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
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
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
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
Re: RMI - ClassCastException - Proxy cannot be cast
Figured it out...
I had to use the same package name (Which makes sense)