我正在編寫一個RMI計算器程序。我創建了以下.java文件。遵循指導原則,我到了必須運行服務應用程序的地步。要做到這一點我型RMI計算器問題(JAVA)
啓動rmiregistry的
和一個新的空白窗口中打開。然後我嘗試通過鍵入啓動計算器服務如下:
的Java CalculatorService的
,然後什麼也沒有發生。
我的指導方針說:「我必須確保我從與存儲服務器相同的目錄運行註冊表。」
你認爲你可以幫助我嗎? 這裏是我的所有代碼:
Calculator.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)
{
}
}
}
*什麼都不會發生?沒有例外?你期望發生什麼? – EJP 2012-04-24 01:11:04