我想调用RMI,但异常被抛出。它有什么问题?
线程“main”中的异常java.rmi.UnmarshalException:错误 解组回归;嵌套异常是: java.lang.ClassNotFoundException:test.Hello(没有安全管理器: RMI类加载器已禁用) sun.rmi.registry.RegistryImpl_Stub.lookup(未知来源)at java.rmi.Naming.lookup(未知来源)at com.xx.rmiproxy.RmiProxy.call(RmiProxy.java:39)at com.xx.rmiproxy.RmiProxy.main(RmiProxy.java:18)引起: java.lang.ClassNotFoundException:test.Hello(没有安全管理器:RMI 类加载器禁用) sun.rmi.server.LoaderHandler.loadProxyClass(未知来源)at java.rmi.server.RMIClassLoader $ 2.loadProxyClass(未知来源)
public String call(String rmiId, String json) throws Exception{
RmiInterfaceDescription desc = dao.getDescriptionById(rmiId);
desc.setJarFile("d:\\test.jar");
RmiClientClassLoader rmiClassLoader = new RmiClientClassLoader(null,desc);
Class interfaceClass = rmiClassLoader.loadClass(desc.getInterfaceName());
List<String> paraClasses = desc.getParaClasses();
Class returnClass = rmiClassLoader.loadClass(desc.getReturnClass());
Object obj = Naming.lookup(desc.getRmiUrl());
Class[] parameterTypes = new Class[paraClasses.size()];
for(int i=0;i<paraClasses.size();i++){
parameterTypes[i]= rmiClassLoader.loadClass(paraClasses.get(i));
}
Method method = interfaceClass.getDeclaredMethod(desc.getMethodName(),
parameterTypes);
Object params[] = parseParamsFromJson();
Object result = method.invoke(obj, "ssd");
return encode(result);
}
.....
public class RmiClientClassLoader extends URLClassLoader {
public static void main(String args[]) throws Exception {
RmiInterfaceDescription description = new RmiInterfaceDescription();
description.setJarFile("d:\\test.jar");
RmiClientClassLoader cl = new RmiClientClassLoader(null, description);
Class clientClass = cl.loadClass("test.client.HelloClient");
}
private String basedir;
private RmiInterfaceDescription description;
@SuppressWarnings("deprecation")
public RmiClientClassLoader(String basedir,
RmiInterfaceDescription description) throws MalformedURLException {
super(new URL[] { new File(description.getJarFile()).toURL() });
this.basedir = basedir;
this.description = description;
}
答案 0 :(得分:0)
你看错了。此异常的重要部分是ClassNotFoundException及其命名的类。是否需要在客户端部署哪个类,如果您将其作为单独的进程运行,则可供注册表使用。
我不知道你为什么要定义自己的类加载器。代码库功能通常就足够了。