【问题标题】:RMI Calculator Issue (JAVA)RMI 计算器问题 (JAVA)
【发布时间】:2012-05-04 04:02:43
【问题描述】:

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

启动 rmiregistry

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

java CalculatorService

然后什么都没有发生。

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

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

计算器.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; 
}

RemoteCalculator.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; 
    } 
}

CalculatorService.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); 

    }
}

CalculatorClient.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)
        {

        }

    }
}

【问题讨论】:

  • 什么都没有发生?没有例外?你预计会发生什么?

标签: java client rmi calculator


【解决方案1】:

这里是步骤。

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

你做了第二步吗?

【讨论】:

  • 是的,在我编译 RemoteCalculator.java 之后我编译了它:rmic RemoteCalculator
猜你喜欢
  • 2016-11-06
  • 2015-08-28
  • 2011-08-18
  • 2021-11-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多