Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RMI Exception: proxy cannot be cast to remote object

Tags:

java

rmi

I'm getting the above error when trying to view the attributes of a remote object on the client side. After reading a post on a forum (http://www.java-forums.org/networking/69581-rmi-classcastexception-proxy-cannot-cast.html) I thought this happened because of the remote object being in different package names in the client and the server, so I moved the client program (and even the remote interface) in the same package with the server and the remote object itself. But I still have the same error:

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

This is my remote interface:

package uk.ac.rm950.nonPublicDataSerializationServer;

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

The remote object class looks like this:

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;
}
}

This is the server program:

package uk.ac.rm950.nonPublicDataSerializationServer;

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(transportingObject, 0);
registry.rebind("employee", stub);

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

This is the client-side code:

 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();
 }}}

The error is thrown as the client program is executed, I can't figure out what I'm doing wrong.

like image 587
Mahab Avatar asked Mar 25 '26 13:03

Mahab


1 Answers

You're trying to cast the stub to the remote object itself. You can't. It isn't. You should be casting it to the remote interface:

ObjectTransporter<Employee>
like image 192
user207421 Avatar answered Mar 27 '26 03:03

user207421



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!