我有以下情况:
使用RMI,我注意到在特定情况下,RMI客户端没有通过RMI发送实例的类。所以,我以Byte []的形式发送了类,然后用我定义的类加载器加载它。
客户端的代码类似于:
pp = plugins.getPanelClasses(); //RMI method
panelClassLoader pcl = new panelClassLoader(); //simple class loader defined by me
String name = pcl.defineClass(pp);
然后,我在客户端询问加载的类的实例:
HashMap<String, PluginPanel> panels = null;
try
{
panels = plugins.getSettingsPanels();
} catch (RemoteException ex)
{
Logger.getLogger(ServerOptions.class.getName()).log(Level.SEVERE, null, ex);
}
PluginPanel是一个接口,其实现类并不总是在客户端...
并且类加载器是:
private class panelClassLoader extends ClassLoader
{
public panelClassLoader(ClassLoader parent)
{
super(parent);
}
public panelClassLoader()
{
super(getSystemClassLoader());
}
public String defineClass(byte[] b)
{
Class c = super.defineClass(null, b, 0, b.length);
resolveClass(c);
return c.getName();
}
}
尽管如此,我仍然遇到以下错误: java.rmi.UnmarshalException:错误解组返回;嵌套异常是: java.lang.ClassNotFoundException:pt.ua.dicoogle.IndexPlugin.CPanel(没有安全管理器:禁用了RMI类加载器) at sun.rmi.server.UnicastRef.invoke(UnicastRef.java:178) at java.rmi.server.RemoteObjectInvocationHandler.invokeRemoteMethod(RemoteObjectInvocationHandler.java:178) at java.rmi.server.RemoteObjectInvocationHandler.invoke(RemoteObjectInvocationHandler.java:132) 在$ Proxy22.getSettingsPanels(未知来源) 在pt.ua.dicoogle.rGUI.client.windows.ServerOptions。(ServerOptions.java:176)
引起:java.lang.ClassNotFoundException:pt.ua.dicoogle.IndexPlugin.CPanel(没有安全管理器:禁用了RMI类加载器) at sun.rmi.server.LoaderHandler.loadClass(LoaderHandler.java:375) at sun.rmi.server.LoaderHandler.loadClass(LoaderHandler.java:165) 在java.rmi.server.RMIClassLoader $ 2.loadClass(RMIClassLoader.java:620) at java.rmi.server.RMIClassLoader.loadClass(RMIClassLoader.java:247) at sun.rmi.server.MarshalInputStream.resolveClass(MarshalInputStream.java:197) at java.io.ObjectInputStream.readNonProxyDesc(ObjectInputStream.java:1574) at java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:1495) at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1731) at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1328) 在java.io.ObjectInputStream.readObject(ObjectInputStream.java:350) 在java.util.HashMap.readObject(HashMap.java:1030) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) 在java.lang.reflect.Method.invoke(Method.java:597) at java.io.ObjectStreamClass.invokeReadObject(ObjectStreamClass.java:974) at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1848) at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1752) at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1328) 在java.io.ObjectInputStream.readObject(ObjectInputStream.java:350) at sun.rmi.server.UnicastRef.unmarshalValue(UnicastRef.java:306) 在sun.rmi.server.UnicastRef.invoke(UnicastRef.java:155)
任何消化?
答案 0 :(得分:2)
您必须在该类加载器的上下文中调用remote方法。不是一个好的计划。详细了解RMI代码库功能,它专为这种情况而设计。