【问题标题】:Running RMI Server, got errors with marshalling arguments and class not found运行 RMI 服务器,出现编组参数错误和找不到类
【发布时间】:2016-07-21 18:04:01
【问题描述】:

我正在学习如何创建 RMI 项目。

但是当我运行服务器站点时,它有错误。当我运行:rmic CalculatorImpl 时,它只是创建存根,我没有看到 ske。

这是我运行 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

这是我的代码:

文件计算器服务器:

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);
    }
}
}

所以请帮帮我。谢谢!

【问题讨论】:

  • 那一定是老教程了。您不再需要运行rmic
  • @erickson 他使用该代码。他需要super(0); 来避免rmic
  • 我试过了,还是报错

标签: java rmi


【解决方案1】:

总结评论链。

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

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

选项二是​​p>

两个配置服务器使用自动下载存根文件这可以通过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

【讨论】:

  • 我在另一个项目中创建了客户端站点。那么,我应该把它移到当前项目吗?
  • 这不是必须的。从我发送给您的链接中阅读第 6.1 点。它包含与您发布的完全相同的错误。
  • 请注意,代码库可以指向一个目录。不一定是 URL
  • 我添加了-Djava.rmi.server.codebase来运行CalculatorServer,但是还是报错
  • 尝试从您的代码中创建注册表 LocateRegistry.createRegistry() 这样服务器和注册表都将在同一个虚拟机中运行
【解决方案2】:

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

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

【讨论】:

  • 但仍然出错:java.rmi.ConnectException: Connection denied to host: localhost;嵌套异常是:java.net.ConnectException:连接被拒绝:连接
  • @EJP 如果你查看我的 cmets,我已经在 1 小时前告诉他了。
  • @AlexanderPetrov 你在开玩笑吧?我应该读60厘米?我读到的只是你的错误答案。很高兴看到你现在已经修复它以同意我的观点。
  • @ejp 这只是 50 个 cmets 中的第 4 个。无论如何,很高兴看到你证实了我的想法,尽管你的回答仍然是片面的。
  • @AlexanderPetrov 是你的。还可以选择使用-classpath 选项启动rmiregistry,或者从特定的工作目录开始,这样. 的默认CLASSPATH 将允许加载存根apclass。我不认为这些有太多实际用途,坦率地说,代码库功能也是如此。
猜你喜欢
  • 1970-01-01
  • 2015-06-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-07-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多