0

我正在编写一个 RMI 计算器程序。我创建了以下 .java 文件。按照指导方针,我到了必须运行服务应用程序的地步。为此,我键入

启动 rmiregistry

并打开一个新的空白窗口。然后我尝试通过键入以下内容来启动计算器服务:

java计算器服务

然后什么也没有发生。

我的指导方针说“我必须确保从与存储服务器的目录相同的目录运行注册表”。

你觉得你能帮我解决这个问题吗?这是我的所有代码:

计算器.java

import java.rmi.Remote;
import java.rmi.RemoteException;

public interface Calculator extends java.rmi.Remote 
{ 
    /*
     * method for addition
     */ 
    public double add(double x, double y) 
            throws RemoteException;

    /*
     * method for subtraction 
     */ 
    public double subtract(double x, double y) 
            throws RemoteException; 
    /* 
     * method for multiplication
     */ 
    public double multiply(double x, double y) 
            throws RemoteException; 
    /* 
     * method for division 
     */ 
    public double divide(double x, double y) 
            throws RemoteException; 
}

远程计算器.java

import java.rmi.server.UnicastRemoteObject; 
import java.rmi.RemoteException;
public class RemoteCalculator extends UnicastRemoteObject implements Calculator
{
    public RemoteCalculator() throws RemoteException
    {

    } 

    /*
     * method for addition
     */ 
    public double add(double x, double y) 
    { 
        return x+y;
    }
    /* 
     * method for subtraction
     */ 
    public double subtract(double x, double y) 
    { 
        return x-y; 
    } 
    /*
     * method for multiplication 
     */ 
    public double multiply(double x, double y) 
    {
        return x*y; 
    }
    /* 
     * method for division 
     */ 
    public double divide(double x, double y)
    { 
        return x/y; 
    } 
}

计算器服务.java

import java.rmi.Naming; 
import java.rmi.RemoteException; 
import java.rmi.RMISecurityManager;

public class CalculatorService 
{ 
    public static void main(String[] args) throws RemoteException,java.net.MalformedURLException
    {
        RemoteCalculator remcalc = new RemoteCalculator(); 
        Naming.rebind("CalcService", remcalc); 

    }
}

计算器客户端.java

import java.rmi.Naming; 
public class CalculatorClient 
{
    public static void main(String[] args)
    { 
        double x = Double.parseDouble(args[1]);
        double y = Double.parseDouble(args[2]);
        try 
        { 
            //Connect to the calculator service 
            Calculator calc = (Calculator) Naming.lookup("rmi://" + args[0] + "/CalcService"); 
            System.out.println("Client bound: OK"); 
            //Add the numbers 
            System.out.println(x + " + " + y + " = " + calc.add(x, y)); 
            //Subtract the numbers 
            System.out.println(x + " - " + y + " = " + calc.subtract(x, y)); 
            //Multiply the numbers 
            System.out.println(x + " * " + y + " = " + calc.multiply(x, y)); 
            //Divide the numbers 
            System.out.println(x + " / " + y + " = " + calc.divide(x, y));

        } 
        catch (java.rmi.NotBoundException nbe) 
        { 
            System.out.println("Client bound: error: " + nbe); 
        }
        catch (java.net.MalformedURLException mue) 
        { 
            System.out.println("Client bound: error: " + mue); 

        } catch (java.rmi.RemoteException re)
        {

        }

    }
}
4

1 回答 1

0

以下是步骤。

  1. 使用 javac 编译
  2. 用 rmic 编译器编译
  3. 启动 rmiregistry
  4. 启动服务器
  5. 启动客户端

你做了第2步吗?

于 2012-04-23T14:37:21.213 回答