设置简单RMI服务器时出现问题

时间:2011-06-25 16:56:24

标签: java rmi

我在设置一个简单的RMI服务器时遇到了问题,并且想知道是否有人可以指出我正确的方向。

我有一个简单的类Server,它应该导出一个简单的接口。目前,当我尝试运行服务器类时,我得到以下异常:

java.rmi.ServerException: RemoteException occurred in server thread; nested exception is: 
java.rmi.UnmarshalException: error unmarshalling arguments; nested exception is: 
java.lang.ClassNotFoundException: prototype.ISimpleService

我已经通过运行“start rmiregistry”命令确保rmi注册表正在运行,并且我使用以下参数运行服务

-Djava.rmi.server.codebase=file:C:/Users/John/Documents/NetBeansProjects/Prototype/build/classes

上面参数中的路径指向我的类层次结构的顶级文件夹,因此:

/类

/原型

- > ISimpleService.class

- > SimpleServiceImpl.class

- > IServer.class

这是我的服务器代码:

public class Server
{
    private static ISimpleService service;

    public static void main(String[] args)
    {
        service = new SimpleServiceImpl();

        try
        {
            ISimpleService stub = (ISimpleService) UnicastRemoteObject.exportObject(service, 0);

            Registry registry = LocateRegistry.getRegistry();

            registry.rebind("SimpleService", stub);

            System.out.println("Simple Service bound");
        } 
        catch (Exception ex)
        {
            System.out.println("Exception binding the simple service");
            ex.printStackTrace();
        }
    }
}

我已经查看了Java RMI跟踪,并调整了我的简单示例以遵循这一点,虽然我没有使用我不应该使用的安全/权限,因为我已经得到了一个RMI示例,之前没有那些东西......

所以我相信我必须错过一些简单的东西(没有双关语),让这项工作,但我现在看不到任何东西。

有人有想法吗?

这是完整的堆栈跟踪:

java.rmi.ServerException: RemoteException occurred in server thread; nested exception is: 
    java.rmi.UnmarshalException: error unmarshalling arguments; nested exception is: 
    java.lang.ClassNotFoundException: prototype.ISimpleService
    at sun.rmi.server.UnicastServerRef.oldDispatch(UnicastServerRef.java:396)
    at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:250)
    at sun.rmi.transport.Transport$1.run(Transport.java:159)
    at java.security.AccessController.doPrivileged(Native Method)
    at sun.rmi.transport.Transport.serviceCall(Transport.java:155)
    at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:535)
    at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:790)
    at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:649)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask    (ThreadPoolExecutor.java:886)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
    at java.lang.Thread.run(Thread.java:662)
    at sun.rmi.transport.StreamRemoteCall.exceptionReceivedFromServer    (StreamRemoteCall.java:255)
    at sun.rmi.transport.StreamRemoteCall.executeCall(StreamRemoteCall.java:233)
    at sun.rmi.server.UnicastRef.invoke(UnicastRef.java:359)
    at sun.rmi.registry.RegistryImpl_Stub.rebind(Unknown Source)
    at prototype.Server.main(Server.java:35)
Caused by: java.rmi.UnmarshalException: error unmarshalling arguments; nested exception is: 
    java.lang.ClassNotFoundException: prototype.ISimpleService
    at sun.rmi.registry.RegistryImpl_Skel.dispatch(Unknown Source)
    at sun.rmi.server.UnicastServerRef.oldDispatch(UnicastServerRef.java:386)
    at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:250)
    at sun.rmi.transport.Transport$1.run(Transport.java:159)
    at java.security.AccessController.doPrivileged(Native Method)
    at sun.rmi.transport.Transport.serviceCall(Transport.java:155)
    at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:535)
    at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:790)
    at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:649)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask    (ThreadPoolExecutor.java:886)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
    at java.lang.Thread.run(Thread.java:662)
Caused by: java.lang.ClassNotFoundException: prototype.ISimpleService
    at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:247)
    at sun.rmi.server.LoaderHandler.loadProxyInterfaces(LoaderHandler.java:711)
    at sun.rmi.server.LoaderHandler.loadProxyClass(LoaderHandler.java:655)
    at sun.rmi.server.LoaderHandler.loadProxyClass(LoaderHandler.java:592)
    at java.rmi.server.RMIClassLoader$2.loadProxyClass(RMIClassLoader.java:628)
    at java.rmi.server.RMIClassLoader.loadProxyClass(RMIClassLoader.java:294)
    at sun.rmi.server.MarshalInputStream.resolveProxyClass(MarshalInputStream.java:238)
    at java.io.ObjectInputStream.readProxyDesc(ObjectInputStream.java:1530)
    at java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:1492)
    at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1731)
    at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1328)
    at java.io.ObjectInputStream.readObject(ObjectInputStream.java:350)
    ... 12 more

2 个答案:

答案 0 :(得分:2)

我已经解决了这个问题......虽然让我的时间比我预期的要长:(

当你像我一样使用代码库参数时,如果你指向一个目录,你应该在末尾放置一个尾随的“/”。一旦我加入,我的服务器就成功绑定了。

有关此资源的信息,请参阅http://download.oracle.com/javase/1.4.2/docs/guide/rmi/codebase.html#section6

答案 1 :(得分:0)

似乎prototype.ISimpleService不在您的类路径中。

尝试:

java -cp=C:/Users/John/Documents/NetBeansProjects/Prototype/build/classes;C:/Users/John/Documents/NetBeansProjects/Server/build/classes Server