繁体   English   中英

RMI 解组异常

[英]RMI UnmarshalException

我目前正在开发一个通过 rmi 加载类的系统。 该系统使用与服务器通信的类加载器来获取类。 代码如下。

服务器:

import rocks.squareRock;

import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;

public class Server extends UnicastRemoteObject
        implements RemInterface {

    public Server() throws RemoteException {
        super();
    }

    public static void main(String argv[]) {
        try {
            Server serv = new Server();
            Naming.rebind("RockServer", serv);
        } catch (Throwable t) {
            t.printStackTrace();
        }
    }

    public Class<?> getRockClass(String type) {
        if (type.equals("squareRock"))
            return squareRock.class;
        else
            return null;
    }
}

客户:

import rocks.Rock;

import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;

public class Client {
    RemInterface reminterface = null;
    RockLoader rl = null;

    public Client() {
        String strName = "rmi://127.0.0.1/RockServer";
        try {
            reminterface = (RemInterface) Naming.lookup(strName);
            rl = new RockLoader(reminterface);
        } catch (RemoteException e) {
            e.printStackTrace();
        } catch (NotBoundException e) {
            e.printStackTrace();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }

        loadRock("squareRock");

    }

    public Rock loadRock(String rock) {
        try {
            return (Rock) rl.loadClass(rock, false).newInstance();
        } catch (Throwable t) {
            return null;
        }
    }
}

界面:

public interface RemInterface {
    public Class<?> getRockClass(String type) throws RemoteException;
}

装载机:

import java.io.Serializable;

public class RockLoader extends ClassLoader implements Serializable {

    private RemInterface reminterface = null;

    public RockLoader(RemInterface reminterface) {
        super();
        this.reminterface = reminterface;
    }

    @Override
    protected synchronized Class<?> loadClass(String className, boolean resolve)
            throws ClassNotFoundException {
        try {
            return reminterface.getRockClass(className);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}

我得到的错误是(客户端):

java.rmi.UnmarshalException: error unmarshalling return; nested exception is: 
java.lang.ClassNotFoundException: SquareRock

这让我很困惑,因为我不是在解组 SquareRock 实例,而是解组 Class。 我唯一的想法是我的类加载器可能是错误的。

不管是 Class 还是 object。 除非您使用 RMI 代码库功能,否则接收 JVM 必须在其类路径中包含该 class。 您所做的基本上是尝试自己实现代码库功能。 你不能那样做。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM