[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 822
  • Last Modified:

RMI AND SSL: java.lang.ClassCastException: $Proxy0

I'm tryiong to run this simple code:

Main.java

>import java.rmi.registry.LocateRegistry;
>import java.rmi.registry.Registry;
>import java.rmi.server.RMIClientSocketFactory;
>import java.rmi.server.RMIServerSocketFactory;
>import java.rmi.server.UnicastRemoteObject;
>
>import javax.rmi.ssl.SslRMIClientSocketFactory;
>import javax.rmi.ssl.SslRMIServerSocketFactory;
>
>public class Main{
>
>      /**
>       * @param args
>       */
>      public static void main(String[] args) {
>            try{
>
>            RemoteObject ro = new RemoteObject();
>          
>            RMIClientSocketFactory csf = new SslRMIClientSocketFactory();
>              RMIServerSocketFactory ssf = new SslRMIServerSocketFactory();
>          
>                  RemoteObject serverStub =(RemoteObject) UnicastRemoteObject.exportObject(ro, 1099, csf, ssf);
>              // create the registry and bind the name and object.
>             Registry registry = LocateRegistry.createRegistry( 2004 );
>                    
>              registry.rebind("remoteObject", serverStub);
>      
>            }catch (Exception e) {
>                  e.printStackTrace();
>            }
>
>      }

The RemoteObject.file has the following code:

>public class RemoteObject implements RemoteObjectInterface{
>
>    public void receiveMessage(String message) throws Exception{
>      System.out.println("Remote: Recieved a message: \"" + message +"\"" );
>    }
>
>}

And the interface code is


>import java.rmi.*;
>import java.util.Vector;
>
>
>public interface RemoteObjectInterface extends Remote{
>
>      public void receiveMessage(String x) throws Exception;
>
>}

I get this exception:

java.lang.ClassCastException: $Proxy0
      at Main.main(Main.java:23)

I really can't understand what it's related to, i'm using JVM v 5.0.

Please it's VERY urgent
0
khzuul
Asked:
khzuul
3 Solutions
 
CEHJCommented:
>>RemoteObject serverStub =(RemoteObject) UnicastRemoteObject.exportObject(ro, 1099, csf, ssf);

exportObject returns Remote, not RemotObject. Should be


Remote serverStub =(Remote) UnicastRemoteObject.exportObject(ro, 1099, csf, ssf);
0
 
objectsCommented:
you don't even need to be making that call.
Your code should look more like the following:

>public class Main{
>
>     /**
>      * @param args
>      */
>     public static void main(String[] args) {
>          try{
>
>       
>          RMIClientSocketFactory csf = new SslRMIClientSocketFactory();
>             RMIServerSocketFactory ssf = new SslRMIServerSocketFactory();
>          RemoteObject ro = new RemoteObject(1099, csf, ssf);
>       
>             // create the registry and bind the name and object.
>            Registry registry = LocateRegistry.createRegistry( 2004 );
>                 
>              registry.rebind("remoteObject", ro);
>     
>          }catch (Exception e) {
>               e.printStackTrace();
>          }
>
>     }

The RemoteObject.file has the following code:

>public class RemoteObject extends UnicastRemoteObject implements MyInterface{
>
      public RemoteObject(int port, RMIClientSocketFactory csf, RMIServerSocketFactory ssf) {
           super(port, csf, ssf);
      }
 
>    public void receiveMessage(String message) throws Exception{
>      System.out.println("Remote: Recieved a message: \"" + message +"\"" );
>    }
>
>}

And the interface code is


>import java.rmi.*;
>import java.util.Vector;
>
>
>public interface MyInterface {
>
>     public void receiveMessage(String x) throws Exception;
>
>}
0
 
matthew016Commented:
U don't need to get the value of
UnicastRemoteObject.exportObject(ro, 0, csf, ssf);

public class Main{

     public static void main(String[] args) {
          try{        
              RMIClientSocketFactory csf = new SslRMIClientSocketFactory();
              RMIServerSocketFactory ssf = new SslRMIServerSocketFactory();
              RemoteObject ro = new RemoteObject();
             UnicastRemoteObject.exportObject(ro, 0, csf, ssf);

            Registry registry = LocateRegistry.createRegistry( 1099);
                 
             registry.rebind("remoteObject", ro);
     
          }catch (Exception e) {
               e.printStackTrace();
          }
}


For your proxy error, check that u have exactly the same Remote interface in client and server
(check for the package declaration, it must be the same too)
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now