RMI 类未找到异常
过去一周我一直在构建一个 RMI 应用程序,但遇到了一个障碍,无论用多少谷歌搜索似乎都无法帮助解决。
以下代码用于通过 RMI 将对象从服务器发送到客户端:
服务器代码:
import rocks.Rock;
import rocks.squareRock;
import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
public class Server extends UnicastRemoteObject
implements RemInterface {
public Server() throws RemoteException {
super();
}
public static void main(String argv[]) {
try {
Server serv = new Server();
Naming.rebind("RockServer", serv);
} catch (Throwable t) {
t.printStackTrace();
}
}
public Rock getRock() {
return new squareRock();
}
}
客户端代码:
import rocks.Rock;
import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
public class Client {
RemInterface reminterface = null;
public Client() {
String strName = "rmi://127.0.0.1/RockServer";
try {
reminterface = (RemInterface) Naming.lookup(strName);
} catch (RemoteException e) {
e.printStackTrace();
} catch (NotBoundException e) {
e.printStackTrace();
} catch (MalformedURLException e) {
e.printStackTrace();
}
}
public Rock loadRock() {
try {
return reminterface.getRock();
} catch (Throwable t) {
return null;
}
}
}
接口:
public interface RemInterface {
public Rock getRock() throws RemoteException;
}
在这种情况下:
- “Rock”类在客户端和服务器类路径中都可用。
- “Rock”类实现了可序列化。
- “squareRock”扩展了 rock 类,并且仅在服务器的类路径中可用。
当尝试在客户端上使用来自 loadRock() 的 Rock 调用方法时出现的错误如下:
STDERR: java.rmi.UnmarshalException: error unmarshalling return; nested exception is:
java.lang.ClassNotFoundException: SquareRock
任何帮助将不胜感激。
I've been building an RMI application over the past week and I've hit a roadblock that no amount of googling can seem to help with.
The following code is used to send an object from the server to the client via RMI:
Server code:
import rocks.Rock;
import rocks.squareRock;
import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
public class Server extends UnicastRemoteObject
implements RemInterface {
public Server() throws RemoteException {
super();
}
public static void main(String argv[]) {
try {
Server serv = new Server();
Naming.rebind("RockServer", serv);
} catch (Throwable t) {
t.printStackTrace();
}
}
public Rock getRock() {
return new squareRock();
}
}
Client code:
import rocks.Rock;
import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
public class Client {
RemInterface reminterface = null;
public Client() {
String strName = "rmi://127.0.0.1/RockServer";
try {
reminterface = (RemInterface) Naming.lookup(strName);
} catch (RemoteException e) {
e.printStackTrace();
} catch (NotBoundException e) {
e.printStackTrace();
} catch (MalformedURLException e) {
e.printStackTrace();
}
}
public Rock loadRock() {
try {
return reminterface.getRock();
} catch (Throwable t) {
return null;
}
}
}
Interface:
public interface RemInterface {
public Rock getRock() throws RemoteException;
}
In this situation:
- The "Rock" class is available in both the Client and Server classpath.
- The "Rock" class implements serializable.
- The "squareRock" extends class rock and is only available in the server's classpath.
The error I get when trying to call a method using a Rock from loadRock() on the client is as follows:
STDERR: java.rmi.UnmarshalException: error unmarshalling return; nested exception is:
java.lang.ClassNotFoundException: SquareRock
Any help would be appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您将从服务器返回一个
rocks.squareRock
类型的对象。在客户端的反序列化过程中,需要此类来创建此类的实例来表示来自服务器的响应。正如您已经指出的那样,该类仅在服务器的类路径中可用,因此无法找到并加载所述类会导致异常。解决方案是让rocks.squareRock 类在客户端也可用。
You are returning an object of Type
rocks.squareRock
from the Server. During the de-serialization process at the client, this class will be required in order to create an instance of this class to represent the response from the server. As you've already indicated that the class is available only in the server's classpath, the failure to locate and load the said class causes the exception.The resolution will be to make the
rocks.squareRock
class available in the client as well.