Java rmi远程异常。RMI不起作用
当我尝试运行我的RMI示例时,我得到一个远程异常。我不明白为什么。 我运行程序时没有任何程序本身或JVM的参数 请帮我排除这个异常 非常感谢 这是我得到的例外:Java rmi远程异常。RMI不起作用,java,eclipse,rmi,Java,Eclipse,Rmi,当我尝试运行我的RMI示例时,我得到一个远程异常。我不明白为什么。 我运行程序时没有任何程序本身或JVM的参数 请帮我排除这个异常 非常感谢 这是我得到的例外: Server exception: java.rmi.ServerException: RemoteException occurred in server thread; nested exception is: java.rmi.UnmarshalException: error unmarshalling argumen
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.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.rmi.server.codebase
参数,该参数列出rmi服务器导出对象所需的所有JAR
请参阅并查看“启动服务器”部分。另外,请务必查看“运行客户端程序”一节中的其他建议参数,注册表或客户端或两者都找不到异常中指定的类。有几种可能的解决方案:
LocateRegistry.createRegistry()
,它解决了类路径问题,并仅在客户端的类路径上提供必要的类客户端可能找不到接口。客户机和服务器都需要有接口的副本,在Java6之前,您还需要使用rmic来构建您尝试使用的客户机存根。这将使RMI非常容易。当您不发送任何参数时,您正在执行
getRegistry(null)
。另外,您正在使用哪些操作系统?如果您在Ubuntu中,127.0.1.1:1099
是主机地址。@EJP感谢您清除它。当我开始做RMI时,我一直在使用127.0.0.1
:/这只是可能的解决办法之一。他根本不“需要”这样做。@EJP真的,因为这是我让它工作的唯一方法是真的。这不是唯一可行的方法。查看我的答案。解决方案1
在测试期间似乎对我不起作用。如果你试图通过一个注册表部署多个解决方案,那么EJP似乎是“首选”的解决方案,但是,你可能会考虑使用多个注册表……只需大声思考;正如你所说,这是一个“A”解决方案;)
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();
}
}
}