Java RMI - ClassNotFound 异常

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13036566/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 11:16:34  来源:igfitidea点击:

Java RMI - ClassNotFound exception

javarmiclassnotfoundexception

提问by Rod

I'm creating a Java RMI program, but I'm getting a ClassNotFoundException. Could you guys help me figure it out? I'm using Eclipse. Somebody suggested me it was a codebase problem, but how does this relate? Here are the codes for my Server and Client:

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

Server:

服务器:

    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();
    }

}

Client:

客户:

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();
    }

}

and the error is this:

错误是这样的:

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.run(Unknown Source)
    at java.net.URLClassLoader.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.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

回答by akshaykumar6

Both client and server should have same package name. I had the same error yesterday, i corrected it after lots of searching. Try it and tell me if any other error comes.

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

Mark this as answer, if you find so.Thank You !!

将此标记为答案,如果您找到了。谢谢!

回答by user207421

CellImpl has not been exported or made available via the client's CLASSPATH. It needs to either (a) extend UnicastRemoteObject or be exported via UnicastRemoteObject.exportObject(); or (b) be Serializable and available on the client's CLASSPATH.

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

回答by samsad beagum

Solution for NoClassFound Exception when running RMI Client.

运行 RMI Client 时 NoClassFound Exception 的解决方法。

Case:

案件:

The server and client files are in different folders.

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

Example:

例子:

Server side: The server interface and server implementation are inside Project Folder: C:\RMIServer Package: rmiserver

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

Client side: The server interface and the client are inside Project Folder: C:\RMIClient Package: rmiClient

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

Problem: Could not find the location of the server interface.

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

Solution: In the client side, 1. Create a package name rmiServer (the package name should be the same as the server side package). 2. Place the server stub inside this package. 3. The client.java is inside the rmiClient package. Now import the rmiServer package in the client.java file. import rmiServer.*;

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

This will solve the classNotFound Exception that happens when we execute the client side RMI.

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