以下方法:
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 中收到一条消息,内容是启动服务器时出现问题,然后出现上述异常。这可能是什么原因?
我不明白为什么最后一个异常声明说当我导入正确的包时找不到类。
最佳答案
此异常有四种情况。
导出时:您没有运行“rmic”,也没有执行
UnicastRemoteObject
的 Javadoc 序言中描述的步骤,从而使其变得不必要。绑定(bind)时:注册表没有 stub 或远程接口(interface)或它们依赖于其类路径的东西。
查找时:客户端的类路径上没有这些东西。
调用远程方法时:您要么向 CLASSPATH 上不存在的类的服务器发送某些内容,要么从不在您的 CLASSPATH 上的类的服务器接收某些内容(包括异常):在这两种情况下可能是远程接口(interface)的方法签名中提到的类或接口(interface)的派生类或接口(interface)实现。
这是情况 2。注册表找不到指定的类。
有几种解决方案:
使用包含相关 JAR 或目录的 CLASSPATH 启动注册表。
通过
LocateRegistry.createRegistry() 在服务器 JVM 中启动注册表。
使用动态 stub ,如
UnicastRemoteObject 的 Javadoc 前言中所述。
但是,您仍然可能会遇到远程接口(interface)本身或其依赖的类的相同问题,在这种情况下,上述 1-3 仍然适用于该类/那些类。确保不会发生上述情况 (4)。
使用代码库功能。这确实是一个部署选项,在我看来,在初始开发阶段应该避免这种情况。
关于java.rmi.ServerException : RemoteException occurred in server thread (ClassNotFoundException),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12087751/