2

RMI の例を実行しようとすると、リモート例外が発生します。理由がわかりません。プログラム自体または JVM への引数なしでプログラムを実行します。

例外を取り除くのを手伝ってください。

どうもありがとう

これは私が得る例外です:

Server exception: java.rmi.ServerException: RemoteException occurred in server thread; nested exception is: 
    java.rmi.UnmarshalException: error unmarshalling arguments; nested exception is: 
    java.lang.ClassNotFoundException: hello.Hello
java.rmi.ServerException: RemoteException occurred in server thread; nested exception is: 
    java.rmi.UnmarshalException: error unmarshalling arguments; nested exception is: 
    java.lang.ClassNotFoundException: hello.Hello

これらは私が持っているクラスです:

クライアントクラス:

package hello;

import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;

public class Client {

    private Client() {}

    public static void main(String[] args) {

    String host = (args.length < 1) ? null : args[0];
    try {
        Registry registry = LocateRegistry.getRegistry(host);
        Hello stub = (Hello) registry.lookup("Hello");
        String response = stub.sayHello();
        System.out.println("response: " + response);
    } catch (Exception e) {
        System.err.println("Client exception: " + e.toString());
        e.printStackTrace();
    }
    }
}

リモート インターフェイス:

package hello;

import java.rmi.Remote;
import java.rmi.RemoteException;

public interface Hello extends Remote {
    String sayHello() throws RemoteException;
}

そして最後にサーバー:

package hello;

import java.rmi.registry.Registry;
import java.rmi.registry.LocateRegistry;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;

public class Server implements Hello {

    public Server() {}

    public String sayHello() {
    return "Hello, world!";
    }

    public static void main(String args[]) {

    try {
        Server obj = new Server();
        Hello stub = (Hello) UnicastRemoteObject.exportObject(obj, 0);

        // Bind the remote object's stub in the registry
        Registry registry = LocateRegistry.getRegistry("localhost");
        registry.bind("Hello", stub);

        System.err.println("Server ready");
    } catch (Exception e) {
        System.err.println("Server exception: " + e.toString());
        e.printStackTrace();
    }
    }
}
4

2 に答える 2

1

レジストリまたはクライアント、あるいはその両方が、例外で指定されたクラスを見つけることができません。考えられる解決策はいくつかあります。

  1. レジストリとクライアントを実行するときに、そのクラスをクラスパスに含めます。そして、それが依存するすべてのクラスは、閉鎖されるまで再帰的に。

  2. を使用してサーバー JVM 内からレジストリを起動するとLocateRegistry.createRegistry()、そのクラスパスの問題が解決され、クライアントのクラスパスにのみ必要なクラスが提供されます。

  3. コードベース機能を使用して、すべてのシステム コンポーネントが必要なサーバー側クラスにアクセスできるようにします。

于 2013-09-26T08:32:18.940 に答える