2010-04-21 96 views
-1

我目前正在使用rmi註冊表使用遠程環境。我的服務器是這樣的:RMI註冊表失敗....異常sais null

 
import java.rmi.registry.LocateRegistry; 
import java.rmi.registry.Registry; 


public class Server { 
    public static void main(String args[]){ 
     if(args.length"); 
      System.exit(-1); 
     }  
     try{ 
      Registry r=LocateRegistry.getRegistry(); 
     MethodsImp methods=new MethodsImp(); 
      //have the object to be remotely accessed so will bind it to the registry 
      System.out.println("Will register on "+args[0]); 
      r.rebind(args[0], methods); 
     } 
     catch(Exception e){ 
      System.out.println("Something went wrong when registring the methods"); 
      System.out.println(e.getMessage()); 
      System.exit(-1); 
     } 

    } 

} 

當我通過運行該程序:

 
java -classpath /home/outsider/Desktop/RIM/RIM_TP1_correct/src -Djava.rmi.server.codebase=file:/home/outsider/Desktop/RIM/RIM_TP1_correct/src/ Server regsiter_name 

我得到這個:

Will register on regsiter_name 
Something went wrong when registring the methods 
null 

出於某種原因,當我用r.rebind它拋出一個具有消息null的異常。 之前嘗試運行的程序我做

rmiregistry &

外殼上安裝rmiregistry中。 我找不到我做錯了什麼。 如果有人可以幫助將是巨大的

+0

拋出什麼類型的異常....?嘗試'System.out.println(e.getClass()。getSimpleName());' – Finbarr 2010-04-21 20:36:51

+0

它給NullPointerException ....它似乎是r.rebind不工作,因爲它應該....但我不知道爲什麼 – 2010-04-21 20:41:56

+0

你的代碼不能編譯,你沒有提供堆棧跟蹤。目前的形式無法形容。 – EJP 2016-05-22 03:58:37

回答

0

與您的代碼的問題是你需要導出遠程對象

MethodsImp方法=新MethodsImp();

到rmi註冊表。

您可以通過包括該代碼

MethodsImp methods=new MethodsImp(); 
Hello stub = (Hello) UnicastRemoteObject.exportObject(methods, 0); 

你好做到這一點是接口的名稱。

此代碼將您的對象導出到註冊表中。如果您正在實現接口並將對象綁定到同一個類中,則不需要導出對象。如果使用不同的類綁定對象,則還需要導出對象。

+0

你不知道。大多數遠程對象擴展「UnicastRemoteObject」,不需要顯式導出。沒有堆棧跟蹤,你不可能知道問題是什麼。 – EJP 2016-05-22 03:58:00

+0

那裏沒有'輸出到RMI註冊表'這樣的東西。有出口,期限,並有*綁定*註冊表。 – EJP 2017-03-17 23:35:57