RMI Calculator プログラムをコーディングしています。次の .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;
}
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)
{
}
}
}