0

我是 java RMI 的新手,我正在按照教程来了解它。它使用服务器,获取服务器的代码清单如下

  1. 计算器服务器.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();
            }
        }
    
  2. 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.计算器.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

4

1 回答 1

0

听起来您没有运行 RMI 注册表,这是与 CalculatorServer 程序分开的进程。它位于您的 java bin 目录中。请稍等片刻,然后再次尝试您的代码。

于 2013-03-29T09:51:14.283 回答