Java RMI客户端方法调用

Java RMI客户端方法调用,java,compiler-errors,rmi,Java,Compiler Errors,Rmi,我是RMI的新手,正在尝试将以下内容应用到我正在从事的项目中。 这段代码是否命名。查找。。。。。。theWork.newCalculator()始终需要位于mainmethod? 我可以调用myCalculatoroutsidemainmethod吗? 当我尝试时,我发现我的计算器无法解决错误。 下面的示例在main中调用myCalculator,使其正常工作。如何使myCalculator.plus(arg)在另一种方法中可用 public static void main(String []

我是RMI的新手,正在尝试将以下内容应用到我正在从事的项目中。
这段代码是否命名。查找。。。。。。theWork.newCalculator()始终需要位于
main
method?
我可以调用
myCalculator
outside
main
method吗?
当我尝试时,我发现
我的计算器无法解决
错误。
下面的示例在
main
中调用
myCalculator
,使其正常工作。如何使
myCalculator.plus(arg)
在另一种方法中可用

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

您已经将myCalculator对象定义为main方法内部的局部变量,这就是为什么如果您尝试在外部引用它,则会出现无法解决的错误

您是否尝试在主方法之外定义myCalculator对象引用,如下所示:-

private static Calculator myCalculator = null;

public static void main(String [] args)
{

        try{


            CalculatorFactory theWorks =     (CalculatorFactory)Naming.lookup("rmi://localhost:13456/CalculationsAnon");
            myCalculator = theWorks.newCalculator();

           // You rest of the code here
这段代码“Naming.lookup……theWork.newCalculator();”是否始终需要在main方法中

没有

我可以在main方法之外调用我的计算器吗

可以,只要您有权访问
theWorks
变量

当我尝试时,我发现
myCalculator无法解决
错误

这是一个编译错误。提到的变量不在范围内。它实际上与RMI毫无关系,只是一个常见的编程错误

下面的示例主要调用myCalculator使其工作。如何使myCalculator.plus(arg)在另一种方法中可用

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

在该方法中执行查找,或将主方法的查找结果存储到静态或实例变量中,而不是存储到局部变量中。

请向我们展示您的尝试。