title: Java how develope a jar plugin with jdbc driver inside
body: <p>I'm developing a plugin jar for my server application. I'd like that this plugin contains jdbc driver inside to access the dbms.</p>
  
  <p>This is the code in the plugin to load the jdbc:</p>
  
  <pre><code>URL url = PLugin.class.getResource("mysql-connector-java-5.1.22.jar");
      log.debug("Url: "+url.getPath());
      URLClassLoader ucl = new URLClassLoader(new URL[] { url }, this.getClass().getClassLoader());
  Class.forName("com.mysql.jdbc.Driver", true, ucl);
  </code></pre>
  
  <p>Executing this code from a main method in the plugin itself works fine.</p>
  
  <p>This is how I load the jar into my server application:</p>
  
  <pre><code>File pluginFile = new File(System.getProperty("user.home") +  File.separator + "plugins" + File.separator + fileJar);       
      if (pluginFile != null &amp;&amp; pluginFile.exists() &amp;&amp; pluginFile.canRead()) {
          try {
              URL[] urls = { new URL("jar:file:" + pluginFile.getAbsolutePath() + "!/") };
              ClassLoader uc = new URLClassLoader(urls, this.getClass().getClassLoader());
  
              JarFile jarFile = new JarFile(pluginFile);
              Enumeration&lt;JarEntry&gt; 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('/', '.');
                  Class clazz = uc.loadClass(className);
  
                  return clazz.newInstance();
              }
          } catch (Exception e) {
              log.error("",e);
          }
      } else {
          log.error("File " + pluginFile.getAbsolutePath() + " not found");
      }
  </code></pre>
  
  <p>In short I read the jar file and I load the first file.class inside (there is only 1 file .class inside every plugin.jar) and then I create an instance of that class (that is the same of the code above.
  The plugins are in a folder outside the classhpath of the server application.</p>
  
  <p>Unfortunally doing this I've always this exception:</p>
  
  <pre><code>24/07/2014 19:26:00 ERROR PluginManager:68 - 
  java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
      at java.net.URLClassLoader$1.run(URLClassLoader.java:372)
      at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
      at java.security.AccessController.doPrivileged(Native Method)
      at java.net.URLClassLoader.findClass(URLClassLoader.java:360)
      at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
      at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
      at java.lang.Class.forName0(Native Method)
      at java.lang.Class.forName(Class.java:340)
      ....
      ....
  </code></pre>
  
  <p>I guess that this is a classpath problem but I'm not sure how can be solved.</p>
  
  <p>Thanks</p>