我试图从Oracle页面运行Hello World RMI示例,但是我一直遇到错误。
我不断得到的错误是
服务器异常:java.rmi.NoSuchObjectException:表中没有这样的对象java.rmi.NoSuchObjectException:sun.rmi.transport.StreamRemoteCall.exceptionReceivedFromServer(StreamRemoteCall.java:275)处的表中没有这样的对象.executeCall(StreamRemoteCall.java:252)在sun.rmi.server.UnicastRef.invoke(UnicastRef.java:378)在sun.rmi.registry.RegistryImpl_Stub.bind(未知源)在example.hello.Server.main(Server) .java:26)
这是直接从我使用的站点获取的代码:
Hello界面:
包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();
}
}
}
任何反馈,我们将不胜感激!
注册表未运行。启动rmiregistry工具,或更改getRegistry()
为createRegistry().
很好奇,因为某些JVM必须一直在侦听端口1099,但没有在其中运行注册表。通常没有注册表会导致java.rmi.ConnectException.
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句