我试图从Oracle页面运行Hello World RMI示例,但我一直在收到错误.
我一直得到的错误是
服务器异常:java.rmi.NoSuchObjectException:表java.rmi.NoSuchObjectException中没有这样的对象:在sun.rmi.transport.StreamRemoteCall的sun.rmi.transport.StreamRemoteCall.exceptionReceivedFromServer(StreamRemoteCall.java:275)中的表中没有这样的对象.executeCall(StreamRemoteCall.java:252)at sun.rmi.server.UnicastRef.invoke(UnicastRef.java:378)at sun.rmi.registry.RegistryImpl_Stub.bind(Unknown Source)at example.hello.Server.main(Server的.java:26)
以下是直接从我使用的网站获取的代码:
Hello接口:
package example.hello;
import java.rmi.Remote;
import java.rmi.RemoteException;
public interface Hello extends Remote {
String sayHello() throws RemoteException;
}
Run Code Online (Sandbox Code Playgroud)
这是我的服务器类:
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();
}
}
}
Run Code Online (Sandbox Code Playgroud)
客户代码:
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();
}
}
}
Run Code Online (Sandbox Code Playgroud)
非常感谢任何反馈!
注册表未运行.要么启动rmiregistry工具,要么getRegistry()改为createRegistry().
这很奇怪,因为一些JVM必须一直在侦听端口1099,而不是在其中运行注册表.通常没有注册表导致java.rmi.ConnectException.