2010-03-10 108 views
1

我想一些非常基本的RMI:Java RMI InitialContext:等效於LocateRegistry.createRegistry(int)?

// Context namingContext = new InitialContext(); 
     Registry reg = LocateRegistry.createRegistry(9999); 
     for (int i = 0; i < objs.length; i++) { 
      int id = objs[i].getID(); 
//   namingContext.bind("rmi:CustomObj" + id , objs[i]); 
      reg.bind("CustomObj" + id , objs[i]); 
     } 

即順利工作,但對未來而言,我需要使用InitialContext

  Context namingContext = new InitialContext(); 
     for (int i = 0; i < objs.length; i++) { 
      int id = objs[i].getID(); 
      namingContext.bind("rmi:CustomObj" + id , objs[i]); 
     } 

但我無法得到這個工作。我從命令行啓動了rmiregistry。有沒有相當於LocateRegistry.createRegistry(int)?或者以其他方式啓動InitialContext從我的課堂使用的RMI註冊表/註冊表?


(而不是命令行的)堆棧跟蹤:

javax.naming.CommunicationException [Root exception is java.rmi.ServerException: RemoteException occurred in server thread; nested exception is: 
     java.rmi.UnmarshalException: error unmarshalling arguments; nested exception is: 
     java.lang.ClassNotFoundException: bguiz.scratch.network.eg.Student] 
     at com.sun.jndi.rmi.registry.RegistryContext.bind(RegistryContext.java:126) 
     at com.sun.jndi.toolkit.url.GenericURLContext.bind(GenericURLContext.java:208) 
     at javax.naming.InitialContext.bind(InitialContext.java:400) 
     at bguiz.scratch.RMITest.main(RMITest.java:29) 
Caused by: java.rmi.ServerException: RemoteException occurred in server thread; nested exception is: 
     java.rmi.UnmarshalException: error unmarshalling arguments; nested exception is: 
     java.lang.ClassNotFoundException: bguiz.scratch.CustomObj 
     at sun.rmi.server.UnicastServerRef.oldDispatch(UnicastServerRef.java:396) 
     at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:250) 
     ....(truncated) 

編輯:我會刪除我自己的問題在一兩天的,因爲似乎沒有回答這個(我自己無法弄清楚)。最後呼籲任何咬人!

回答

1

經過多次修補,我已經解決了這個問題。 FYI,這是什麼:

ClassNotFoundException由於RMI註冊表有它自己的類路徑而被拋出。包含InitialContext的類在其類路徑中具有自定義對象並不重要 - 必須對RMI註冊表進行初始化,以使自定義對象的類路徑爲以及

爲此,請在啓動rmiregistry之前在comman行上設置classpath環境值。如果此類路徑包含自定義對象的類,則不會拋出ClassNotFoundException,並且隨後可以避免使用ServerException和CommunicationException。

0

是否有相當於LocateRegistry.createRegistry(int)

或者一些其他的方式,從我的類中開始通過InitialContext的使用RMI註冊/註冊?

只有LocateRegistry.createRegistry()

我幾乎可以肯定你需要在URL中指定主機名。你有什麼異常&錯誤信息?

+0

@EJP我已經添加了堆棧跟蹤到我的問題。另外,這個類將在服務器上運行 - 因此不需要指定URL(它將是本地主機)。客戶端需要指定URL,並且由於我在同一臺機器上進行了沙箱處理,所以它指定了「rmi:// localhost:1099 /」',但是我還沒有那麼遠,因爲這個類無法運行。 – bguiz 2010-03-10 10:02:29

0
java.lang.ClassNotFoundException: bguiz.scratch.network.eg.Student caused by 
java.lang.ClassNotFoundException: bguiz.scratch.CustomObj 

檢查此類是否可用?

+0

是的,它絕對是 - 這是我檢查的第一件事情之一。事實上,數組'objs []'是一個'CustomObj'數組,它被同一個類使用。我也證實它在類路徑中。 – bguiz 2010-03-11 08:22:31