openfire 部署后报错: java.lang.IllegalArgumentException: interface xx is not visible from class loader

该异常是创建代理时加载接口的类加载器与创建时传入的不一致。

在本地eclipse做openfire二次开发,本地运行没错,部署到服务器上后报异常:

     java.lang.IllegalArgumentException: interface xx is not visible from class loader。

根据异常信息,可知是动态代理时出错的。而在之前部署过却没有这异常发生。

从日志上分析,可以找到抛异常的地方是:

Class<?> java.lang.reflect.Proxy.getProxyClass0(ClassLoader loader, Class<?>... interfaces);
 
由于服务器上不能断点分析,以及无法修改jdk添加日志,通过复制关键代码到自己代码开始抛出异常处打印日志,发现:
            Class<?> interfaceClass = null;
            try {
                interfaceClass = Class.forName(interfaceName, false, loader);
            } catch (ClassNotFoundException e) {
            }
            if (interfaceClass != interfaces[i]) {
                throw new IllegalArgumentException(
                    interfaces[i] + " is not visible from class loader");
            }        
interfaceClass 为null值。

然后对比之前的开发代码:
之前:
public RPCGameAction gameAction = (RPCGameAction) Container
            .createRemoteService(RPCGameAction.class, "gamecenter");



现在:
public RPCGameAction getGameAction(String prefix)
    {
        
        return (RPCGameAction) Container
                .createRemoteService(RPCGameAction.class, prefix);
    }

而创建代理的代码中的获取类加载器的代码为:
ClassLoader loader = Thread.currentThread().getContextClassLoader();

 前者会在初始化的时候已经创建好,而后者会根据运行时的ClassLoader而创建,而openfire加载插件的类加载器跟运行的不一致,从而导致创建失败。

解决的方法可以是:RPCGameAction 这个代理随插件启动而创建到内存中;

         把接口所处的jar包放到../openfire/lib 目录下。

 
原文地址:https://www.cnblogs.com/qingyibusi/p/7910941.html