【发布时间】:2019-07-01 18:01:02
【问题描述】:
我想用两台笔记本电脑编写一个 RMI 应用程序,这两个数字相加?我已经将一台笔记本电脑作为我的服务器,另一台笔记本电脑作为我的客户端。当我们想定义从远程接口扩展的接口时,我应该在哪台机器上定义这个接口是客户端还是服务器端?请帮忙。
我已经使用一台机器制作了一个 RMI 应用程序,它工作正常我已经在同一个包中定义了接口,但是当我在不同的机器上工作时它不起作用。
public interface AdditionI extends Remote {
public int add(int x ,int y) throws RemoteException;
}
public class Server extends UnicastRemoteObject implements AdditionI {
public Server() throws RemoteException {}
@Override
public int add(int x, int y) throws RemoteException {
return x+y;
}
public static void main(String ar [])throws RemoteException {
try
{
Registry reg = LocateRegistry.createRegistry(2177);
reg.rebind("Add", new Server());
System.out.println("Server is ready");
}
catch(Exception e)
{
System.out.println("Error "+ e);
}
}
}
public class Client {
public static void main(String ar[])throws RemoteException {
try {
Registry reg = LocateRegistry.getRegistry("localhost",2177);
AdditionI ad = (AdditionI)reg.lookup("Add");
System.out.println("REsult:"+(ad.add(10, 5)));
} catch (Exception e) {
System.out.println("Error"+e);
}
}
}
当我在同一台机器上运行此代码时,它工作正常,显示方法 add 的结果,但在不同的机器上它显示以下消息。
java.rmi.ServerException: RemoteException occurred in server thread; nested exception is: java.rmi.UnmarshalException: error unmarshalling arguments; nested exception is: java.lang.ClassNotFoundException:
【问题讨论】:
-
您是否尝试过将 AdditionI 接口单独编译为 Jar,然后在服务器和客户端计算机上包含所述 jar?
-
两者都不是。使用共享包。
-
澄清请求:您的问题得到充分回答了吗?如果是这样,请考虑accepting 一个答案。这样做有助于未来的读者确定问题是否已解决,并对花时间回答您的人表示感谢。否则请提供反馈,以便人们进一步调整他们的答案。