我正在创建一个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
答案 0 :(得分:2)
客户端和服务器都应具有相同的包名称。我昨天遇到了同样的错误,经过大量搜索我纠正了它。尝试一下,告诉我是否还有其他错误。
如果您发现,请将此标记为答案。谢谢!
答案 1 :(得分:1)
CellImpl尚未通过客户端的CLASSPATH导出或提供。它需要(a)扩展UnicastRemoteObject或通过UnicastRemoteObject.exportObject()导出;或(b)可序列化并可在客户的CLASSPATH上获得。
答案 2 :(得分:-2)
运行RMI客户端时出现NoClassFound异常的解决方案。
案例:
服务器和客户端文件位于不同的文件夹中。
示例:
服务器端:服务器接口和服务器实现在里面 项目文件夹:C:\ RMIServer 套餐:rmiserver
客户端:服务器接口和客户端在里面 项目文件夹:C:\ RMIClient 包装:rmiClient
问题:无法找到服务器界面的位置。
解决方案:在客户端, 1.创建包名称rmiServer(包名称应与服务器端包相同)。 2.将服务器存根放在此包中。 3. client.java位于rmiClient包中。现在导入client.java文件中的rmiServer包。 import rmiServer。*;
这将解决当我们执行客户端RMI时发生的classNotFound异常。