1

I created a simple Java RMI application in netbeans 7.0.1 ( win 7 + jdk 7u1). I have two separate projects:

RMI_Client contains:

MyClient class:

package rmi_client;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;

public class MyClient 
{
     public static void main(String[] args) 
     {
          String msg = "Hello RMI";
          rmiInterface stub;
          Registry reg;

          try
          {
               reg = LocateRegistry.getRegistry("localhost");
               stub = (rmiInterface) reg.lookup("Hello");

               try
               {
                    stub.hello(msg);
               }
               catch(Exception e)
               {
                    System.out.println("Remote method exception thrown: " +e.getMessage());   
               }

          }
          catch(Exception e)
          {
               System.err.println("Client exception thrown: " + e.toString());
               e.printStackTrace();
          }
     }
 }

rmiInterface interface:

package rmi_client;
import java.rmi.Remote;
import java.rmi.RemoteException;

public interface rmiInterface extends Remote 
{
     void hello(String message) throws RemoteException;
}

RMI_Server contains:

rmiInterface interface - exactly the same as above

rmiImpl class:

package rmi_server;
import java.rmi.RemoteException;

public class rmiImpl implements rmiInterface 
{
     @Override
     public void hello(String message) throws RemoteException 
     {
          System.out.println("Server:" + message);
     }
}

MyServer class:

package rmi_server;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.UnicastRemoteObject;

public class MyServer   
{
     public static void main(String[] args) 
     {
          try
          {
               rmiImpl robj = new rmiImpl();
               rmiInterface stub = (rmiInterface) UnicastRemoteObject.exportObject(robj, 0);
               Registry reg = LocateRegistry.createRegistry(1099);
               reg.rebind("Hello", stub);
               System.out.println("Server is ready to listen: ");
          } 
          catch (Exception e)
          {
               System.err.println("Server exception thrown: " + e.toString());
          }

     }
}

If I'm doing something wrong above please let me know. First I start RMI_Server application, then when I run RMI_Client I get errors:

Client exception thrown: java.rmi.UnmarshalException: error unmarshalling return; nested exception is: 
    java.lang.ClassNotFoundException: rmi_server.rmiInterface (no security manager: RMI class loader disabled)
java.rmi.UnmarshalException: error unmarshalling return; nested exception is: 
    java.lang.ClassNotFoundException: rmi_server.rmiInterface (no security manager: RMI class loader disabled)
    at sun.rmi.registry.RegistryImpl_Stub.lookup(Unknown Source)
    at rmi_client.MyClient.main(MyClient.java:28)
Caused by: java.lang.ClassNotFoundException: rmi_server.rmiInterface (no security manager: RMI class loader disabled)
    at sun.rmi.server.LoaderHandler.loadProxyClass(LoaderHandler.java:554)
    at java.rmi.server.RMIClassLoader$2.loadProxyClass(RMIClassLoader.java:646)
    at java.rmi.server.RMIClassLoader.loadProxyClass(RMIClassLoader.java:311)
    at sun.rmi.server.MarshalInputStream.resolveProxyClass(MarshalInputStream.java:257)
    at java.io.ObjectInputStream.readProxyDesc(ObjectInputStream.java:1549)
    at java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:1511)
    at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1750)
    at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1347)
    at java.io.ObjectInputStream.readObject(ObjectInputStream.java:369)
    ... 2 more

Where is the problem ?

frictionlesspulley
  • 11,070
  • 14
  • 66
  • 115
user1052836
  • 329
  • 1
  • 4
  • 14

1 Answers1

1

RMI_Server contains:

rmiInterface interface - exactly the same as above

No it isn't. This message:

java.lang.ClassNotFoundException: rmi_server.rmiInterface

says that rmiinterface is in the rmi_server package, which isn't 'the same as above'.

It has to be exactly the same class. Not a similar class in a different package.

So what you need is three packages: server, client, and shared. The shared package needs to contain the remote interface and any application classes it relies on.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • But how to resolve that problem when client and server work on two different machines ? – user1052836 Nov 18 '11 at 01:35
  • @user1052836 The shared .jar file must deployed to both machines. – user207421 Nov 18 '11 at 02:15
  • I created rmi_shared.jar which contains rmiInterface and I added it to both projects. Now the application works but I get a messeage "Server: Hello RMI" in RMI_Server netbeans console under the "Server is ready to listen:". The message "Server: Hello RMI" should be printed in RMI_Client netbeans console. So something is wrong. – user1052836 Nov 18 '11 at 11:28
  • I think that the function hello from rmiImpl should return some value (in this case String), then my client can print a message (System.out.println(remoteObject.hello(msg)); – user1052836 Nov 18 '11 at 12:42
  • @user1052836 Exactly. Remote methods execute at the server, that's the whole point of them. There is no rational reason to expect the server's console output to magically appear at the client. – user207421 Nov 18 '11 at 23:23