2013-09-26 89 views
1

當我嘗試運行我的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(); 
    } 
    } 
} 
+0

很可能客戶端找不到接口。客戶端和服務器都需要有接口的副本,並且在Java 6之前,您還需要使用rmic來構建客戶端stubby – MadProgrammer

+0

您是否嘗試過使用[Spring-Remoting](http://stackoverflow.com/問題/ 19017734 /我 - 布爾總是自帶向上糾正/ 19017859#19017859)。這將使RMI變得非常簡單。 – TheKojuEffect

+0

當你沒有參數發送時,你正在做'getRegistry(null)'。另外,你正在使用哪個操作系統?如果你在Ubuntu中,'127.0.1.1:1099'是主機地址。 – TheKojuEffect

回答

-1

正如RMI trail

您可能需要提供java.rmi.server.codebase參數列出所有的RMI服務器需要導出的對象的罐子概述..

請參閱Running the examples並查看「正在啓動服務器」部分。另外請務必查看運行客戶端程序的部分以獲取更多建議參數

+0

這只是可能的解決方案之一。他根本不需要這樣做。 – EJP

+0

@EJP真的,因爲這是我唯一能讓它工作的唯一方式 – MadProgrammer

+0

是的。它不是唯一可行的方式。看到我的答案。 – EJP

1

註冊表或客戶端或兩者都找不到在例外中指定的類。有幾種可能的解決方案:

  1. 在執行註冊表和客戶端時在類路徑中包含該類。所有依賴它的類,遞歸直到關閉。

  2. 從服務器JVM中啓動註冊表,其中LocateRegistry.createRegistry()解決了該類路徑問題,並僅在客戶端的類路徑上提供必要的類。

  3. 使用代碼庫功能確保所有系統組件都可以訪問所需的服務器端類。