Java RMI InitialContext:相当于 LocateRegistry.createRegistry(int)?
我正在尝试一些非常基本的 RMI:
// 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)
编辑:我将在几天内删除我自己的问题,因为似乎没有答案(我自己无法弄清楚)。任何咬人者的最后呼吁!
I am trying to some pretty basic RMI:
// 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] );
}
That works without a hitch, but for future purposes, I need to use 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] );
}
But I cannot get this to work. I have started rmiregistry
from the command line. Is there an equivalent of LocateRegistry.createRegistry(int)
? Or some other way to start the RMI registry / registry used by InitialContext
from inside my class? (Instead of the command line)
Stack trace:
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)
EDIT: I will delete my own question in a couple of days, as there seems to be no answer to this (I haven't been able to figure it out myself). Last call for any biters!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
经过一番修改,我已经解决了这个问题。仅供参考,情况如下:
由于 RMI 注册表有自己的类路径,因此引发了
ClassNotFoundException
。包含InitialContext
的类在其类路径上具有自定义对象并不重要 - 必须初始化 RMI 注册表,以便自定义对象也位于其类路径上 。为此,请在启动
rmiregistry
之前在命令行上设置classpath
环境值。如果此类路径包含自定义对象的类,则不会引发ClassNotFoundException
,并且随后会避免ServerException
和 `CommunicationException'。After much tinkering, I've solved the problem. FYI, here's what it was:
The
ClassNotFoundException
is getting thrown because of RMI registry has its own classpath. It doesn't matter that the class containing theInitialContext
has the custom objects on its classpath - The RMI registry must be initialised such that the custom objects are on its classpath as well.To do this set the
classpath
environment value on the comman line prior to startingrmiregistry
. If this classpath contains the custom object's class, theClassNotFoundException
is not thrown, and subsequentlyServerException
and `CommunicationException' are avoided.检查这个课程是否可用?
Check if this class is available ?
不。
仅
LocateRegistry.createRegistry()
。我几乎可以肯定您需要在 URL 中指定主机名。什么例外&您收到错误消息吗?
No.
Only
LocateRegistry.createRegistry()
.I'm almost sure you need to specify the hostname in the URL. What exception & error message are you getting?