一千萬個為什麽

搜索

RMI - 在運行時獲取ClassNotFoundException

我花了將近五個小時試圖解決這個問題,但無濟於事。我創建了一個使用RMI的應用程序。編譯很好,但我不能讓它運行。 我的所有類文件都在C:\ Users \ Benji \ Desktop \ ass2 \ build中(“賦值”的縮寫;沒有任何臟)。所有源文件都在C:\ Users \ Benji \ Desktop \ ass2 \ src中。 我已將所有內容放在一個包中以使事情更易於理解(並更改源中的import語句以反映這一點)。

我已將批處理文件放在C:\ Users \ Benji \ Desktop \ ass2 \中。它包含執行語句:

java -classpath ./build -Djava.rmi.server.codebase=file:/C:/Users/Benji/Desktop/ass2/build -Djava.security.policy=broker.policy BrokerReception Broker 16890

(程序需要兩個“經紀人”和“16890”)。

文件broker.policy也位於C:\ Users \ Benji \ Desktop \ ass2 \中。其內容是:

grant
{
    permission java.security.AllPermission;
};

(是的,我意識到這不是一個好的安全政策。我稍後會討論這個問題)。

實際上有三個Main類,一個用於客戶端,一個用於代理(客戶端的中介)和服務器。我正在嘗試啟動經紀人。 Broker接口的代碼如下:

import java.io.FileNotFoundException;
import java.rmi.Remote;
import java.rmi.RemoteException;
import java.util.ArrayList;

public interface Broker extends Remote
{
    public boolean getAvailability(int startDate, int endDate) throws FileNotFoundException, RemoteException;

    public ArrayList getCities() throws FileNotFoundException, RemoteException;

    public ArrayList getCityHotels(int cityNumber) throws FileNotFoundException, RemoteException;

    public int getHotelRoomRate(int hotelNumber) throws FileNotFoundException, RemoteException;

    public boolean makeBooking(String firstName, String lastName, String contact, String creditCardNo) throws FileNotFoundException, RemoteException;
}

和實現類:

import java.io.FileNotFoundException;
import java.util.ArrayList;

public class BrokerClientLiaison implements Broker
{
    private BrokerDatabase directory;
    private BrokerHotelsLiaison liaison;

    public BrokerClientLiaison(BrokerDatabase directory, int activeHotelNumber)
    {
        this.liaison = new BrokerHotelsLiaison(activeHotelNumber);
        this.directory = directory;
    }

    public boolean getAvailability(int startDate, int endDate) throws FileNotFoundException
    {
        return liaison.getAvailability(startDate, endDate);
    }

    public ArrayList getCities() throws FileNotFoundException
    {
        return directory.getCities();
    }

    public ArrayList getCityHotels(int cityNumber) throws FileNotFoundException
    {
        return directory.getCityHotels(cityNumber);
    }

    public int getHotelRoomRate(int hotelNumber) throws FileNotFoundException
    {
        return liaison.getHotelRoomRate(hotelNumber);
    }

    public boolean makeBooking(String firstName, String lastName, String contact, String creditCardNo) throws FileNotFoundException
    {
        return liaison.makeBooking(firstName, lastName, contact, creditCardNo);
    }
}

最後,啟動實現類的主類:

import java.io.FileNotFoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.UnicastRemoteObject;
import java.sql.SQLException;

public class BrokerReception
{
    public static void main(String[] args)
    {
        System.out.println("Args are:");
        for(String arg : args)
        {
            System.out.println(arg);
        }
        System.out.println();

        try
        {
            BrokerDatabase directory = new BrokerDatabase();
            directory.connect(args[0]);

            int activeHotelNumber = Integer.parseInt(args[1]);
            if(directory.checkActiveHotelExists(activeHotelNumber))
            {
                BrokerClientLiaison liaison = new BrokerClientLiaison(directory, activeHotelNumber);
                Broker liaisonStub = (Broker) UnicastRemoteObject.exportObject(liaison, 0);
                Registry registry = LocateRegistry.getRegistry();
                registry.rebind(Protocol.BROKER_INTERFACE_NAME, liaisonStub);
            }
            else
            {
                throw new FileNotFoundException();
            }
        }
        catch(ArrayIndexOutOfBoundsException aioobe)
        {
            System.err.println("Args required:");
            System.err.println("1. Name of database file");
            System.err.println("2. Number of active hotel");
            System.exit(1);
        }
        catch(ClassNotFoundException cnfe)
        {
            System.err.println("Couldn't load database driver");
            System.exit(2);
        }
        catch(SQLException sqle)
        {
            System.err.println("Couldn't establish connection to database");
            System.err.println("Check that the database has been properly registerd,");
            System.err.println("and that you provided the correct name");
            System.exit(3);
        }
        catch(NumberFormatException nfe)
        {
            System.err.println("Second argument must be an integer");
            System.exit(4);
        }
        catch(FileNotFoundException fnfe)
        {
            System.err.println("The database contains no entries with that hotel number");
            System.exit(5);
        }
        catch(RemoteException re)
        {
            System.err.println("Unable to bind as " + Protocol.BROKER_INTERFACE_NAME);
            re.printStackTrace();
            System.exit(6);
        }
    }
}

上述代碼中的“目錄”是訪問數據庫的類。

我不知道我需要提供什麽其他信息。誰能告訴我我做錯了什麽? 順便說一句,我回去並在 http://download.oracle.com/javase/1.5.0/docs/guide/rmi/hello/hello-world.html ,看看我是否能弄清楚出了什麽問題。本教程未提及代碼庫或安全策略,但提供了編譯和執行的所有代碼和精確指令。我按照這些說明寫了這封信,但即使這樣也行不通!

堆棧跟蹤:

java.rmi.ServerException: RemoteException occurred in server thread; nested exce
ption is:
        java.rmi.UnmarshalException: error unmarshalling arguments; nested excep
tion is:
        java.lang.ClassNotFoundException: Broker
        at sun.rmi.server.UnicastServerRef.oldDispatch(UnicastServerRef.java:396
)
        at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:250)
        at sun.rmi.transport.Transport$1.run(Transport.java:159)
        at java.security.AccessController.doPrivileged(Native Method)
        at sun.rmi.transport.Transport.serviceCall(Transport.java:155)
        at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:5
35)
        at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTranspor
t.java:790)
        at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport
.java:649)
        at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExec
utor.java:886)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor
.java:908)
        at java.lang.Thread.run(Thread.java:662)
        at sun.rmi.transport.StreamRemoteCall.exceptionReceivedFromServer(Stream
RemoteCall.java:255)
        at sun.rmi.transport.StreamRemoteCall.executeCall(StreamRemoteCall.java:
233)
        at sun.rmi.server.UnicastRef.invoke(UnicastRef.java:359)
        at sun.rmi.registry.RegistryImpl_Stub.rebind(Unknown Source)
        at BrokerReception.main(BrokerReception.java:32)
Caused by: java.rmi.UnmarshalException: error unmarshalling arguments; nested ex
ception is:
        java.lang.ClassNotFoundException: Broker
        at sun.rmi.registry.RegistryImpl_Skel.dispatch(Unknown Source)
        at sun.rmi.server.UnicastServerRef.oldDispatch(UnicastServerRef.java:386
)
        at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:250)
        at sun.rmi.transport.Transport$1.run(Transport.java:159)
        at java.security.AccessController.doPrivileged(Native Method)
        at sun.rmi.transport.Transport.serviceCall(Transport.java:155)
        at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:5
35)
        at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTranspor
t.java:790)
        at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport
.java:649)
        at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExec
utor.java:886)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor
.java:908)
        at java.lang.Thread.run(Thread.java:662)
Caused by: java.lang.ClassNotFoundException: Broker
        at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
        at java.lang.Class.forName0(Native Method)
        at java.lang.Class.forName(Class.java:247)
        at sun.rmi.server.LoaderHandler.loadProxyInterfaces(LoaderHandler.java:7
11)
        at sun.rmi.server.LoaderHandler.loadProxyClass(LoaderHandler.java:655)
        at sun.rmi.server.LoaderHandler.loadProxyClass(LoaderHandler.java:592)
        at java.rmi.server.RMIClassLoader$2.loadProxyClass(RMIClassLoader.java:6
28)
        at java.rmi.server.RMIClassLoader.loadProxyClass(RMIClassLoader.java:294
)
        at sun.rmi.server.MarshalInputStream.resolveProxyClass(MarshalInputStrea
m.java:238)
        at java.io.ObjectInputStream.readProxyDesc(ObjectInputStream.java:1530)
        at java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:1492)
        at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1
731)
        at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1328)
        at java.io.ObjectInputStream.readObject(ObjectInputStream.java:350)
        ... 12 more

最佳答案

如果您將註冊表作為第二個進程運行,則需要訪問遠程類。最簡單的方法是在啟動註冊表時向命令行添加適當的類路徑參數。

如果您嘗試使用遠程類加載,我相信您需要在應用程序中設置rmi安全管理器,無論是在命令行還是在main方法中。 (就個人而言,分發課程通常適用於99%的情況,並且容易100倍正確)。

轉載註明原文: RMI - 在運行時獲取ClassNotFoundException