【发布时间】:2013-03-20 00:14:48
【问题描述】:
我是 Java RMI 的新手,我正在按照教程学习它。它使用服务器,代码清单如下所示,用于访问服务器
-
CalculatorServer.java
public class CalculatorServer { public CalculatorServer(){ try { Calculator c = new CalculatorImpl(); Naming.rebind("rmi://localhost:1099/CalculatorService", c); } catch (Exception e) { System.out.println("Trouble"+e); } } public static void main(String args[]){ new CalculatorServer(); } } -
CalculatorImpl.java
public class CalculatorImpl extends UnicastRemoteObject implements Calculator { //constructor public CalculatorImpl() throws RemoteException { super(); } //@Override public long add(long a, long b) throws RemoteException { return a + b; } //@Override public long sub(long a, long b) throws RemoteException { return a - b; } //@Override public long mul(long a, long b) throws RemoteException { return a * b; } // @Override public long div(long a, long b) throws RemoteException { return a / b; } }
3.Calculator.java
public interface Calculator extends Remote{
public long add(long a, long b) throws RemoteException;
public long sub(long a, long b) throws RemoteException;
public long mul(long a, long b) throws RemoteException;
public long div(long a, long b) throws RemoteException;
}
当我调试程序时,这是 netbeans ide 控制台的错误; 它说这个错误:Troublejava.rmi.ServerException:RemoteException发生在服务器线程中;嵌套异常是: java.rmi.UnmarshalException:解组参数错误;嵌套异常是: java.lang.ClassNotFoundException: rmi.Calculator
【问题讨论】:
-
rmiregistry 的类路径中必须有接口类。
-
可以发一下客户端代码吗?