簡體   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