4

我正在创建一个 Java RMI 程序,但我得到了一个 ClassNotFoundException。你们能帮我弄清楚吗?我正在使用 Eclipse。有人建议我这是一个代码库问题,但这有什么关系?以下是我的服务器和客户端的代码:

服务器:

    package server;

import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.UnicastRemoteObject;

import base.Server;
import base.RmiStarter;

public class ServerImplStarter extends RmiStarter{

    public ServerImplStarter() {
        super(Server.class);
    }

    @Override
    public void doCustomRmiHandling() {
        try{
            Server engine = new ServerImpl();
            Server engineStub = (Server)UnicastRemoteObject.exportObject(engine, 0);


            Registry registry = LocateRegistry.createRegistry( 1099 );
            registry = LocateRegistry.getRegistry();
            registry.rebind("Server", engineStub);

        }catch(Exception e){
            e.printStackTrace();
        }

    }

    public static void main(String[] args){
        new ServerImplStarter();
    }

}

客户:

package client;

import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;

import base.RmiStarter;
import base.Server;
import base.Cell;

public class CellClient extends RmiStarter {

    public CellClient() {
        super(Server.class);
    }

    @Override
    public void doCustomRmiHandling() {
        try{
            Registry registry = LocateRegistry.getRegistry();
            Server server = (Server)registry.lookup("Server");

            Cell c = null;
            c = server.getcell();

        }catch(Exception e){
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        new CellClient();
    }

}

错误是这样的:

java.rmi.UnmarshalException: error unmarshalling return; nested exception is: 
    java.lang.ClassNotFoundException: server.CellImpl
    at sun.rmi.server.UnicastRef.invoke(Unknown Source)
    at java.rmi.server.RemoteObjectInvocationHandler.invokeRemoteMethod(Unknown Source)
    at java.rmi.server.RemoteObjectInvocationHandler.invoke(Unknown Source)
    at $Proxy0.getcell(Unknown Source)
    at client.CellClient.doCustomRmiHandling(CellClient.java:23)
    at base.RmiStarter.<init>(RmiStarter.java:19)
    at client.CellClient.<init>(CellClient.java:13)
    at client.CellClient.main(CellClient.java:31)
Caused by: java.lang.ClassNotFoundException: server.CellImpl
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Unknown Source)
    at sun.rmi.server.LoaderHandler.loadClass(Unknown Source)
    at sun.rmi.server.LoaderHandler.loadClass(Unknown Source)
    at java.rmi.server.RMIClassLoader$2.loadClass(Unknown Source)
    at java.rmi.server.RMIClassLoader.loadClass(Unknown Source)
    at sun.rmi.server.MarshalInputStream.resolveClass(Unknown Source)
    at java.io.ObjectInputStream.readNonProxyDesc(Unknown Source)
    at java.io.ObjectInputStream.readClassDesc(Unknown Source)
    at java.io.ObjectInputStream.readOrdinaryObject(Unknown Source)
    at java.io.ObjectInputStream.readObject0(Unknown Source)
    at java.io.ObjectInputStream.readObject(Unknown Source)
    at sun.rmi.server.UnicastRef.unmarshalValue(Unknown Source)
    ... 8 more
4

3 回答 3

2

客户端和服务器都应该具有相同的包名称。我昨天也遇到了同样的错误,经过大量搜索后我更正了。试试看,如果有任何其他错误出现,请告诉我。

将此标记为答案,如果您发现是这样。谢谢!

于 2012-10-24T16:08:09.287 回答
1

CellImpl 尚未通过客户端的 CLASSPATH 导出或提供。它需要 (a) 扩展 UnicastRemoteObject 或通过 UnicastRemoteObject.exportObject() 导出;或 (b) 可序列化并在客户端的 CLASSPATH 上可用。

于 2012-10-23T22:53:29.680 回答
-2

运行 RMI 客户端时 NoClassFound 异常的解决方案。

案子:

服务器和客户端文件位于不同的文件夹中。

例子:

服务器端:服务器接口和服务器实现在项目文件夹:C:\RMIServer 包:rmiserver

客户端:服务器接口和客户端都在里面 项目文件夹:C:\RMIClient 包:rmiClient

问题:找不到服务器接口的位置。

解决方法:在客户端, 1.创建一个包名rmiServer(包名要和服务端包名相同)。2. 将服务器存根放入此包中。3. client.java 在 rmiClient 包内。现在在 client.java 文件中导入 rmiServer 包。导入 rmiServer.*;

这将解决我们执行客户端 RMI 时发生的 classNotFound 异常。

于 2013-05-03T10:07:32.970 回答