java.rmi.ServerException : RemoteException occurred in server thread (ClassNotFoundException)

标签 java exception rmi

以下方法:

    private void startServer() { // snippet that starts the server on the local machine
        try {
             RemoteMethodImpl impl = new RemoteMethodImpl();
             Naming.rebind( "Illusive-Server" , impl );
        } catch(Exception exc) {
            JOptionPane.showMessageDialog(this, "Problem starting the server", "Error", JOptionPane.ERROR_MESSAGE);
            System.out.println(exc);
        }
    }

抛出此异常:

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

当我启动我的项目时,我在 JOptionPane 中收到一条消息,内容是启动服务器时出现问题,然后出现上述异常。这可能是什么原因?

我不明白为什么最后一个异常声明说当我导入正确的包时找不到类

最佳答案

此异常有四种情况。

  1. 导出时:您没有运行“rmic”,也没有执行 UnicastRemoteObject 的 Javadoc 序言中描述的步骤,从而使其变得不必要。

  2. 绑定(bind)时:注册表没有 stub 或远程接口(interface)或它们依赖于其类路径的东西。

  3. 查找时:客户端的类路径上没有这些东西。

  4. 调用远程方法时:您要么向 CLASSPATH 上不存在的类的服务器发送某些内容,要么从不在您的 CLASSPATH 上的类的服务器接收某些内容(包括异常):在这两种情况下可能是远程接口(interface)的方法签名中提到的类或接口(interface)的派生类或接口(interface)实现。

这是情况 2。注册表找不到指定的类。

有几种解决方案:

  1. 使用包含相关 JAR 或目录的 CLASSPATH 启动注册表。

  2. 通过 LocateRegistry.createRegistry() 在服务器 JVM 中启动注册表。

  3. 使用动态 stub ,如 UnicastRemoteObject 的 Javadoc 前言中所述。但是,您仍然可能会遇到远程接口(interface)本身或其依赖的类的相同问题,在这种情况下,上述 1-3 仍然适用于该类/那些类。

  4. 确保不会发生上述情况 (4)。

  5. 使用代码库功能。这确实是一个部署选项,在我看来,在初始开发阶段应该避免这种情况。

关于java.rmi.ServerException : RemoteException occurred in server thread (ClassNotFoundException),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12087751/

相关文章:

java-如何处理运行时错误?

java - Android 自定义通知 RemoView 不使用样式

Java 8 性能对比。 Java 7

Java 术语 : Why compile-time error and not compile-time exception?

java - 如何在Java中进行并行编程?

java - 静态方法以及如何使用它们

java - Jersey:让客户端抛出相同的服务器异常

文件 ://method 上的 java.net.UnknownHostException

java rmi - 访问被拒绝 ("java.net.SocketPermission" "127.0.0.1:1099" "connect,resolve")

java.net.ConnectException :connection timed out: connect? 异常