0

I have created a plugin application and need to pass multiple dependency Jars from a folder dynamically at runtime for JDBC connection.

When using single jar, works fine.,

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);
} 

But in my case, How to load all classes of all jars in a directory and use for the "JDBC connection" driver with this method :

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

How to implement it using URLClassLoader && Class.forName() ??

Thanks in advance,

~ Praz Solver

Praz Solver
  • 513
  • 3
  • 12
  • It looks you're redoing what is already provided with [OPS4J PAX JDBC](https://github.com/ops4j/org.ops4j.pax.jdbc). The only piece you're missing then is how to get OSGi Bundles into the framework for which [Apache Felix File Install](https://felix.apache.org/documentation/subprojects/apache-felix-file-install.html) is a pretty common option (e.g. used by Apache Karaf), which let's you configure one or more directories and it then watches them for OSGi Bundles being dropped there and installs them (if not deactivated). – Ancoron Apr 13 '19 at 09:23
  • Please read the JDBC chapter of OSGi Compendium spec to get an idea how to use JDBC within OSGi: https://osgi.org/specification/osgi.cmpn/7.0.0/service.jdbc.html. After that, you can find implementations of this chapter at OPS4J as @Ancoron already mentioned. – Balazs Zsoldos Apr 15 '19 at 14:52

0 Answers0