user1052836
user1052836

Reputation: 329

Java RMI application failed to run

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 ?

Upvotes: 1

Views: 1938

Answers (1)

user207421
user207421

Reputation: 310840

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.

Upvotes: 1

Related Questions