Java RMI InitialContext:相当于 LocateRegistry.createRegistry(int)?

发布于 2024-08-24 18:00:55 字数 2035 浏览 9 评论 0原文

我正在尝试一些非常基本的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

平定天下 2024-08-31 18:00:55

经过一番修改,我已经解决了这个问题。仅供参考,情况如下:

由于 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 the InitialContext 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 starting rmiregistry. If this classpath contains the custom object's class, the ClassNotFoundException is not thrown, and subsequently ServerException and `CommunicationException' are avoided.

奢欲 2024-08-31 18:00:55
java.lang.ClassNotFoundException: bguiz.scratch.network.eg.Student caused by
java.lang.ClassNotFoundException: bguiz.scratch.CustomObj

检查这个课程是否可用?

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

Check if this class is available ?

貪欢 2024-08-31 18:00:55

是否有等价的LocateRegistry.createRegistry(int)

不。

或者其他方式从我的类内部启动 InitialContext 使用的 RMI 注册表/注册表?

LocateRegistry.createRegistry()

我几乎可以肯定您需要在 URL 中指定主机名。什么例外&您收到错误消息吗?

Is there an equivalent of LocateRegistry.createRegistry(int)?

No.

Or some other way to start the RMI registry / registry used by InitialContext from inside my class?

Only LocateRegistry.createRegistry().

I'm almost sure you need to specify the hostname in the URL. What exception & error message are you getting?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文