Java RMI应用程序无法运行

时间:2011-11-17 23:41:35

标签: java rmi

我在netbeans 7.0.1(win 7 + jdk 7u1)中创建了一个简单的Java RMI应用程序。我有两个独立的项目:

RMI_Client包含:

MyClient类:

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界面:

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

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

RMI_Server包含:

rmiInterface接口 - 与上面完全相同

rmiImpl课程:

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类:

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

     }
}

如果我上面做错了,请告诉我。首先我启动RMI_Server应用程序,然后当我运行RMI_Client时出现错误:

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

问题出在哪里?

1 个答案:

答案 0 :(得分:1)

  

RMI_Server包含:

     

rmiInterface接口 - 与上面完全相同

不,不是。这条消息:

java.lang.ClassNotFoundException: rmi_server.rmiInterface

表示rmiinterface位于rmi_server包中,与上述内容不同。

它必须是完全相同的类。在不同的包中不是类似的类。

所以你需要的是三个包:服务器,客户端和共享。共享包需要包含远程接口和它依赖的任何应用程序类。