【发布时间】:2013-09-26 01:16:41
【问题描述】:
当我尝试运行我的 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();
}
}
}
【问题讨论】:
-
很可能是客户端找不到接口。客户端和服务器都需要有接口的副本,在 Java 6 之前,您还需要使用 rmic 来构建客户端存根
-
您是否尝试过使用Spring-Remoting。这将使 RMI 变得非常容易。
-
当你没有发送参数时,你正在做
getRegistry(null)。另外,您使用的是哪个操作系统?如果您在 Ubuntu 中,127.0.1.1:1099是主机地址。 -
@EJP 感谢您清除它。当我开始使用 RMI 时,我一直坚持使用
127.0.0.1。 ://