连接到java 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.可

  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控制台的错误;它说这个错误:麻烦java.rmi. serverexception: RemoteException发生在服务器线程;嵌套异常是:java.rmi.UnmarshalException:解组参数错误;嵌套异常是:java.lang.ClassNotFoundException: rmi。计算器

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

相关内容

  • 没有找到相关文章

最新更新