當我嘗試運行我的RMI示例時,出現遠程異常。我不明白爲什麼。 我運行的程序沒有任何參數到程序本身或JVM。rmi遠程異常。 RMI不起作用
請幫我擺脫例外。
非常感謝
這是例外,我得到:
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();
}
}
}
遠程interace :
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來構建客戶端stubby – MadProgrammer
您是否嘗試過使用[Spring-Remoting](http://stackoverflow.com/問題/ 19017734 /我 - 布爾總是自帶向上糾正/ 19017859#19017859)。這將使RMI變得非常簡單。 – TheKojuEffect
當你沒有參數發送時,你正在做'getRegistry(null)'。另外,你正在使用哪個操作系統?如果你在Ubuntu中,'127.0.1.1:1099'是主機地址。 – TheKojuEffect