首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >远程管理接口UnmarshalException

远程管理接口UnmarshalException
EN

Stack Overflow用户
提问于 2011-05-25 04:43:48
回答 1查看 2.6K关注 0票数 1

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

服务器:

代码语言:javascript
复制
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;
    }
}

客户端:

代码语言:javascript
复制
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;
        }
    }
}

接口:

代码语言:javascript
复制
public interface RemInterface {
    public Class<?> getRockClass(String type) throws RemoteException;
}

RockLoader:

代码语言:javascript
复制
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;
        }
    }
}

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

代码语言:javascript
复制
java.rmi.UnmarshalException: error unmarshalling return; nested exception is: 
java.lang.ClassNotFoundException: SquareRock

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

EN

回答 1

Stack Overflow用户

发布于 2011-05-25 12:10:32

它是一个类还是一个对象都无关紧要。接收JVM的类路径中必须有该类,除非您正在使用RMI代码库特性。您所做的基本上就是尝试自己实现代码库功能。你不能这么做。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6116646

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档