为运行时库充电

时间:2016-07-28 19:41:30

标签: java reflection jar module classloader

在运行时加载库(.jar)有禁忌吗?

我找到了这样做的代码:

    URLClassLoader sysloader = (URLClassLoader) ClassLoader.getSystemClassLoader();
    Class sysclass = URLClassLoader.class;

    try {
        Method method = sysclass.getDeclaredMethod("addURL", new Class[]{URL.class});
        method.setAccessible(true);
        method.invoke(sysloader, new Object[]{URL_LOAD});
    } catch (Throwable t) {
        t.printStackTrace();
        throw new IOException("Error, could not add URL to system classloader");
    }

此代码有效。但这是最好的方式吗?

我想在运行时加载新罐子可以放在一个文件夹中,从而为我的系统带来新功能。好像它们是附加模块一样。

0 个答案:

没有答案