kamranzafar / JCL

Jar Class Loader, a configurable and dynamic custom classloader designed to create, manage and manipulate isolated Java classloaders in IoC frameworks and web applications.
http://kamranzafar.github.com/
579 stars 161 forks source link

JCL to load plugin.jar with driver inside #27

Open drenda opened 10 years ago

drenda commented 10 years ago

Hi, I'm a server side Java app (run with maven jetty) and I need to load, when required, a plugin.jar that is outside the classpath. I do this with this code:

JarClassLoader jcl = new JarClassLoader(); jcl.add(pluginFile.getPath()); JclObjectFactory factory = JclObjectFactory.getInstance();

            // Create object of loaded class

            JarFile jarFile = new JarFile(pluginFile);
            Enumeration<JarEntry> e = jarFile.entries();
            while (e.hasMoreElements()) {
                JarEntry je = (JarEntry) e.nextElement();
                if (je.isDirectory() || !je.getName().endsWith(".class")) {
                    continue;
                }
                // -6 because of .class
                String className = je.getName().substring(0, je.getName().length() - 6);
                className = className.replace('/', '.');
                Object obj = factory.create(jcl, className);
                log.debug("Classe restituita: "+obj);
                return obj;
            }

then I invoke a method from this plugin class, say doSomething().

My plugin class in short make an access to a datasource (Dmbs) and inside is is included the jar needed to access the db.

in my doSomething() method I do this:

JarClassLoader jcl = new JarClassLoader();
jcl.add("."); JclObjectFactory factory = JclObjectFactory.getInstance(); // Create object of loaded class Object obj = factory.create(jcl, "com.mysql.jdbc.Driver"); Class.forName(dataSet.getDriver(), true, jcl);

Unfortunally when my server app try ti invoke doSomething() method I've always this exception:

26/07/2014 10:17:01 ERROR PluginManager:70 - java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/db at java.sql.DriverManager.getConnection(DriverManager.java:689) at java.sql.DriverManager.getConnection(DriverManager.java:247)

Jcl is supposed to solve this situation?

Thanks