RMI没有这样的对象异常

时间:2014-04-28 05:47:07

标签: java rmi

我试图从Oracle页面运行Hello World RMI示例,但我不断收到错误。

我一直得到的错误是

  

服务器异常:java.rmi.NoSuchObjectException:没有这样的对象   table java.rmi.NoSuchObjectException:表中没有这样的对象   sun.rmi.transport.StreamRemoteCall.exceptionReceivedFromServer(StreamRemoteCall.java:275)     在   sun.rmi.transport.StreamRemoteCall.executeCall(StreamRemoteCall.java:252)     在sun.rmi.server.UnicastRef.invoke(UnicastRef.java:378)at   sun.rmi.registry.RegistryImpl_Stub.bind(未知来源)at   example.hello.Server.main(Server.java:26)

以下是直接从我使用的网站获取的代码:

Hello Interface:

package example.hello;

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

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

这就是我的服务器类:

package example.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();
        }
    }
}

客户代码:

package example.hello;

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

public class Client {

    private Client() {}

    public static void main(String[] args) {

        String host = "localhost";
        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();
        }
    }
}

非常感谢任何反馈!

2 个答案:

答案 0 :(得分:5)

注册表未运行。启动rmiregistry工具,或将getRegistry()更改为createRegistry().

这很奇怪,因为一些JVM必须一直在侦听端口1099,而不是在其中运行注册表。通常,没有注册表会导致java.rmi.ConnectException.

答案 1 :(得分:-4)

我认为您的界面方法应该有公共修饰符