运行RMI服务器,在编组参数和类未找到时出错

时间:2016-07-21 18:04:01

标签: java rmi

我正在研究如何创建RMI项目。

但是当我运行Server站点时,它有错误。当我运行:rmic CalculatorImpl,它只是创建存根,我没有看到ske。 enter image description here

这是我运行rmic CalculatorImpl时的日志:

Warning: generation and use of skeletons and static stubs for JRMP is deprecated. Skeletons are unnecessary, and static stubs have been superseded by dynamically generated stubs. Users are encouraged to migrate away from using rmic to generate skeletons and static stubs. See the documentation for java.rmi.server.UnicastRemoteObject.

当我运行CalculatorServer时:

java.rmi.ServerException: RemoteException occurred in server thread; nested exception is:
java.rmi.UnmarshalException: error unmarshalling arguments; nested exception is:
java.lang.ClassNotFoundException: CalculatorImpl_Stub

这是我的代码:

文件CalculatorServer:

public class CalculatorServer {
public CalculatorServer() {
    try {
        Calculator c = new CalculatorImpl();
        Naming.bind("rmi://localhost:1099/CalculatorService", c);
    } catch (Exception e) {
        System.out.println("Trouble: " + e);
    }
}

public static void main(String args[]) {
    new CalculatorServer();
}
}

文件计算器:

public interface Calculator extends Remote {
public long add(long a, long b)
        throws java.rmi.RemoteException;
}

并且文件为CalculatorImpl:

public class CalculatorImpl extends
    java.rmi.server.UnicastRemoteObject implements Calculator {
public CalculatorImpl()
        throws java.rmi.RemoteException {
    super();
}

public long add(long a, long b)
        throws java.rmi.RemoteException {
    return a + b;
}
}

另一个项目中的CalculatorClient:

public class CalculatorClient {

public static void main(String[] args) {
    try {
        Calculator c = (Calculator)
                Naming.lookup(
                        "rmi://localhost/CalculatorService");
        System.out.println( c.add(4, 5) );

    }
    catch (MalformedURLException murle) {
        System.out.println();

        System.out.println(
                "MalformedURLException");
        System.out.println(murle);
    }
    catch (RemoteException re) {
        System.out.println();
        System.out.println(
                "RemoteException");
        System.out.println(re);
    }
    catch (NotBoundException nbe) {
        System.out.println();
        System.out.println(
                "NotBoundException");
        System.out.println(nbe);
    }
    catch (
            java.lang.ArithmeticException
                    ae) {
        System.out.println();
        System.out.println(
                "java.lang.ArithmeticException");
        System.out.println(ae);
    }
}
}

所以请帮助我。全部!

2 个答案:

答案 0 :(得分:0)

总结评论链。

重要的是注册表应该知道服务器存根。为了实现这一点,有两种选择。选项一是注册表和服务器共享同一个VM。这可以通过

来实现

添加LocateRegistry.createRegistry() 在服务器主类的开头。

选项二是

两个配置服务器使用自动下载存根文件这可以通过java.rmi.server.codebase实现

示例命令

java -cp c:\home\ann\src;c:\home\ann\public_html\classes\compute.jar
     -Djava.rmi.server.codebase=file:/c:/home/ann/public_html/classes/compute.jar
     -Djava.rmi.server.hostname=mycomputer.example.com
     -Djava.security.policy=server.policy
        engine.ComputeEngine

取自https://docs.oracle.com/javase/tutorial/rmi/running.html

答案 1 :(得分:0)

您在注册表的CLASSPATH上没有存根。解决这个问题的简单方法是通过LocateRegistry.createRegistry()在服务器JVM中启动注册表。

然后您可能会发现您在客户端CLASSPATH中也没有。