简体   繁体   English

RMI 解组异常

[英]RMI UnmarshalException

I'm currently developing a system that loads classes via rmi.我目前正在开发一个通过 rmi 加载类的系统。 This system uses a classloader that communicates with the server in order to get the classes.该系统使用与服务器通信的类加载器来获取类。 The code is as follows.代码如下。

Server:服务器:

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

Client:客户:

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

Interface:界面:

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

RockLoader:装载机:

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

The error I'm getting with this is (client-side):我得到的错误是(客户端):

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

This confuses me, as I'm not unmarshalling a SquareRock instance, but a Class.这让我很困惑,因为我不是在解组 SquareRock 实例,而是解组 Class。 The only thought I have is that my classloader might be wrong.我唯一的想法是我的类加载器可能是错误的。

It doesn't matter whether it's a Class or an object.不管是 Class 还是 object。 The receiving JVM must have that class in its classpath, unless you are using the RMI codebase feature.除非您使用 RMI 代码库功能,否则接收 JVM 必须在其类路径中包含该 class。 What you are doing is basically trying to implement the codebase feature yourself.您所做的基本上是尝试自己实现代码库功能。 You can't do that.你不能那样做。

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

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