我是RMI的新手,尝试将以下内容应用到正在处理的项目中。
这段代码Naming.lookup...... theWork.newCalculator();
总是需要在main
方法中吗?
可以拨打myCalculator
以外main
方法吗?
当我尝试时,我得到myCalculator cannot be resolved
错误。
以下示例在main
中调用myCalculator
,以便能够正常工作。如何使myCalculator.plus(arg)
在另一种方法中可用?RMI客户端方法调用
public static void main(String [] args)
{
try{
CalculatorFactory theWorks = (CalculatorFactory)Naming.lookup("rmi://localhost:13456/CalculationsAnon");
Calculator myCalculator = theWorks.newCalculator();
System.out.println("I have a calculator");
int val = 0;
myCalculator.clear();
BufferedReader bin = new BufferedReader(new InputStreamReader(System.in));
for(;;)
{
System.out.println(": "+val+":");
System.out.print("Command>");
String s = (bin.readLine().trim());
if(s.equals("+")){
System.out.print("Value>");
int arg = 0;
s=(bin.readLine().trim());
arg = Integer.parseInt(s);
val = myCalculator.plus(arg);
}
// more codes here
请告诉我们你已经尝试过了。 – 2014-08-31 09:07:25