1

我正在尝试一些非常基本的 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)

编辑:我将在几天后删除我自己的问题,因为似乎没有答案(我自己无法弄清楚)。最后呼吁任何咬人!

4

3 回答 3

1

经过多次修补,我已经解决了这个问题。仅供参考,它是这样的:

ClassNotFoundException由于 RMI 注册表有自己的类路径,因此被抛出。包含 的类在其类路径上具有自定义对象并不重要InitialContext- 必须初始化 RMI 注册表,以便自定义对象也在其类路径上。

为此,请classpath在启动之前在命令行上设置环境值rmiregistry。如果这个类路径包含自定义对象的类,ClassNotFoundException则不会抛出,并且随后ServerException会避免 `CommunicationException'。

于 2010-03-26T10:53:32.503 回答
0

有没有等价物LocateRegistry.createRegistry(int)

不。

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

只有LocateRegistry.createRegistry().

我几乎可以肯定您需要在 URL 中指定主机名。您收到什么异常和错误消息?

于 2010-03-10T09:55:00.777 回答
0
java.lang.ClassNotFoundException: bguiz.scratch.network.eg.Student caused by
java.lang.ClassNotFoundException: bguiz.scratch.CustomObj

检查这个类是否可用?

于 2010-03-10T12:25:11.630 回答