使用URLClassLoader将目录中的所有jar动态加载到驱动程序

时间:2019-04-13 07:17:46

标签: java jar osgi urlclassloader simba

我已经创建了一个插件应用程序,并且需要在运行时动态地从文件夹传递多个依赖Jars,以进行JDBC连接。

使用单个jar时,效果很好。

try {       
    URL u = new URL("jar:file:"/path/to/file.jar"!/");
    URLClassLoader ucl = new URLClassLoader(new URL[] { u });

    Driver d;
    try {
            d = (Driver)Class.forName("com.example.xxx.Driver", true, ucl).newInstance();
        } catch (ClassNotFoundException e) {
            logger.log(Level.ERROR, e.getMessage(), e);
        }
    DriverManager.registerDriver(new DriverDelegator(d));
} catch (Exception e) {
    throw new AdapterException(e);
} 

但是对于我来说,如何将所有jar的所有类加载到目录中,并通过以下方法将其用于“ JDBC连接”驱动程序:

d = (Driver)Class.forName("com.example.xxx.Driver", true, ucl).newInstance();

如何使用 URLClassLoader && Class.forName()

预先感谢

〜Praz Solver

0 个答案:

没有答案