2

尝试连接到 RMI 服务时出现错误:

我正在使用以下命令执行客户端代码:

java -Djava.security.manager -Djava.security.policy=path\to\policy.all -jar "path\to\jarfile" "localhost:2020"

我正在使用的代码是:

public class PowerServiceClient
{
    public static void main(String args[]) throws Exception
    {

         System.setSecurityManager
            (new RMISecurityManager());

         // Call registry for PowerService
         PowerService service = (PowerService) Naming.lookup
            ("rmi://localhost:2020/PowerService");

         DataInputStream din = new
             DataInputStream (System.in);

    }
}

我收到的错误是:

    Exception in thread "main" java.rmi.UnmarshalException: 
    error unmarshalling return; nested exception is:

    java.lang.ClassNotFoundException: rmiservice.PowerService
    at sun.rmi.registry.RegistryImpl_Stub.lookup(Unknown Source)
    at java.rmi.Naming.lookup(Unknown Source)
    at powerclient.PowerServiceClient.main(PowerServiceClient.java:32)
    Caused by: java.lang.ClassNotFoundException: rmiservice.PowerService
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Unknown Source)
    at sun.rmi.server.LoaderHandler.loadProxyInterfaces(Unknown Source)
    at sun.rmi.server.LoaderHandler.loadProxyClass(Unknown Source)
    at sun.rmi.server.LoaderHandler.loadProxyClass(Unknown Source)
    at java.rmi.server.RMIClassLoader$2.loadProxyClass(Unknown Source)
    at java.rmi.server.RMIClassLoader.loadProxyClass(Unknown Source)
    at sun.rmi.server.MarshalInputStream.resolveProxyClass(Unknown Source)
    at java.io.ObjectInputStream.readProxyDesc(Unknown Source)
    at java.io.ObjectInputStream.readClassDesc(Unknown Source)
    at java.io.ObjectInputStream.readOrdinaryObject(Unknown Source)
    at java.io.ObjectInputStream.readObject0(Unknown Source)
    at java.io.ObjectInputStream.readObject(Unknown Source)
    ... 3 more

有谁知道这里有什么问题?

4

1 回答 1

1

Registry 需要访问您的远程接口类及其对类路径的依赖关系。对此有三种解决方案。为了方便:

  1. 使用 LocateRegistry.createRegistry() 在服务器 JVM 中启动注册表
  2. 使用 -D-Jclasspath= 参数启动 rmiregistry 进程
  3. 使用代码库功能。
于 2010-11-19T02:39:32.033 回答