连接到Java RMI服务器

时间:2013-03-29 09:43:58

标签: java netbeans rmi

我是java RMI的新手,我正在按照教程了解它。它使用服务器,代码列表如下所示,以获取服务器

  1. 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();
            }
        }
    
  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. 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

1 个答案:

答案 0 :(得分:0)

听起来您没有运行RMI注册表,这是一个与您的CalculatorServer程序不同的进程。它位于您的java bin目录中。给它一点时间启动,然后再次尝试你的代码。