3 Replies Latest reply: Sep 22, 2010 1:34 AM by DarrylBurke RSS

    Add a jar file to Java load path at run time

    807580
      Hi

      I loaded my file successfully , but when I tried to use the driver to connect to the DB , I get


      java.lang.ClassNotFoundException: com.ibm.db2.jcc.DB2Driver

      Here is my class




      import java.net.URL;
      import java.io.IOException;
      import java.net.URLClassLoader;
      import java.net.MalformedURLException;
      import java.sql.Connection;
      import java.sql.DriverManager;
      import java.io.*;

      public class JarFileLoader1 extends URLClassLoader
      {
      public JarFileLoader1 (URL[] urls)
      {
      super (urls);
      }

      public void addFile (String path) throws MalformedURLException
      {
      String urlPath = "jar:file://" + path + "!/";
      addURL (new URL (urlPath));

      }

      public static void main (String args[])
      {

      try
      {

      File f = new File("E:\\db2_v9_5 FP5_drivers\\db2jcc.jar");
      System.out.println("%%%% " + f.exists());

      File f1 = new File("E:\\db2_v9_5 FP5_drivers\\db2jcc_license_cu.jar");
      System.out.println("%%%% " + f1.exists());

      File f2 = new File("E:\\db2_v9_5 FP5_drivers\\db2jcc4.jar");
      System.out.println("%%%% " + f2.exists());


      URL urls [] = {};

      JarFileLoader1 cl = new JarFileLoader1 (urls);

      cl.addFile ("E:\\db2_v9_5 FP5_drivers\\db2jcc.jar");
      cl.addFile ("E:\\db2_v9_5 FP5_drivers\\db2jcc_license_cu.jar");
      cl.addFile ("E:\\db2_v9_5 FP5_drivers\\db2jcc4.jar");

      URL url = new File("E:\\db2_v9_5 FP5_drivers\\db2jcc.jar").toURL();

      URLClassLoader clazzLoader = new URLClassLoader(new URL[]url);
      Class clazz = clazzLoader.loadClass("com.ibm.db2.jcc.DB2Driver");

      System.out.println ("Success! --> " + clazz.newInstance().toString());

      String connectString = "jdbc:db2://dummy:34000/dev1";

      System.out.println("BEFORE CONNECTION");
      Connection conn =
      DriverManager.getConnection(connectString,"mario","123123");
      System.out.println("after CONNECTION");


      System.out.println("Driver Version - " + conn.getMetaData().getDriverVersion() + "
      ");


      }
      catch (Exception ex)
      {
      System.out.println ("In Exception Block -- Failed.");
      ex.printStackTrace (System.out);
      }
      }
      }


      >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
      >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

      Here are the logging messages

      %%%% true
      %%%% true
      %%%% true
      Success! --> com.ibm.db2.jcc.DB2Driver@24442444
      BEFORE CONNECTION
      In Exception Block -- Failed.
      java.sql.SQLException: No suitable driver
      at java.sql.DriverManager.getConnection(DriverManager.java:582)
      at java.sql.DriverManager.getConnection(DriverManager.java:186)
      at com.tdbfg.tdsecurities.kasper.admin.aboutkasper.JarFileLoader1.main(JarFileLoader1.java:61)
        • 1. Re: Add a jar file to Java load path at run time
          807580
          kasper123 wrote:
          Hi

          I loaded my file successfully , but when I tried to use the driver to connect to the DB , I get


          java.lang.ClassNotFoundException: com.ibm.db2.jcc.DB2Driver

          Here is my class

          import java.net.URL;
          import java.io.IOException;
          import java.net.URLClassLoader;
          import java.net.MalformedURLException;
          import java.sql.Connection;
          import java.sql.DriverManager;
          import java.io.*;
          
          public class JarFileLoader1 extends URLClassLoader
          {
          public JarFileLoader1 (URL[] urls)
          {
          super (urls);
          }
          
          public void addFile (String path) throws MalformedURLException
          {
          String urlPath = "jar:file://" + path + "!/";
          addURL (new URL (urlPath));
          
          }
          
          public static void main (String args[])
          {
          
          try
          {
          
          File f = new File("E:\\db2_v9_5 FP5_drivers\\db2jcc.jar");
          System.out.println("%%%% " + f.exists());
          
          File f1 = new File("E:\\db2_v9_5 FP5_drivers\\db2jcc_license_cu.jar");
          System.out.println("%%%% " + f1.exists());
          
          File f2 = new File("E:\\db2_v9_5 FP5_drivers\\db2jcc4.jar");
          System.out.println("%%%% " + f2.exists());
          
          
          URL urls [] = {};
          
          JarFileLoader1 cl = new JarFileLoader1 (urls);
          
          cl.addFile ("E:\\db2_v9_5 FP5_drivers\\db2jcc.jar");
          cl.addFile ("E:\\db2_v9_5 FP5_drivers\\db2jcc_license_cu.jar");
          cl.addFile ("E:\\db2_v9_5 FP5_drivers\\db2jcc4.jar");
          
          URL url = new File("E:\\db2_v9_5 FP5_drivers\\db2jcc.jar").toURL();
          
          URLClassLoader clazzLoader = new URLClassLoader(new URL[]{url});
          Class clazz = clazzLoader.loadClass("com.ibm.db2.jcc.DB2Driver");
          
          System.out.println ("Success! --> " + clazz.newInstance().toString());
          
          String connectString = "jdbc:db2://dummy:34000/dev1";
          
          System.out.println("BEFORE CONNECTION");
          Connection conn = 
          DriverManager.getConnection(connectString,"mario","123123");
          System.out.println("after CONNECTION");
          
          
          System.out.println("Driver Version - " + conn.getMetaData().getDriverVersion() + " ");
          
          
          }
          catch (Exception ex)
          {
          System.out.println ("In Exception Block -- Failed.");
          ex.printStackTrace (System.out);
          }
          }
          }
          ================================================
          ====================================================

          Here are the logging messages
          %%%% true
          %%%% true
          %%%% true
          Success! --> com.ibm.db2.jcc.DB2Driver@24442444
          BEFORE CONNECTION
          In Exception Block -- Failed.
          java.sql.SQLException: No suitable driver
          at java.sql.DriverManager.getConnection(DriverManager.java:582)
          at java.sql.DriverManager.getConnection(DriverManager.java:186)
          at com.tdbfg.tdsecurities.kasper.admin.aboutkasper.JarFileLoader1.main(JarFileLoader1.java:61)
          For debugging purposes you could use [DriverManager.getDrivers()|http://download.oracle.com/javase/6/docs/api/java/sql/DriverManager.html#getDrivers%28%29] to get an enumeration of all drivers
          and then output their names.

          Maybe, with the classloader malarkey you are doing
          you need to use [DriverManager.registerDriver(Driver driver)|http://download.oracle.com/javase/6/docs/api/java/sql/DriverManager.html#registerDriver%28java.sql.Driver%29]
          • 2. Re: Add a jar file to Java load path at run time
            jschellSomeoneStoleMyAlias
            1, There is a jdbc forum and a reflections forum.

            2, Loading a jdbc driver via a class loader requires some additional tricks to get it to register. This is discussed some where in the jdbc forum.

            3. You should be able to call the driver class directly and use the connect() method from it.
            • 3. Re: Add a jar file to Java load path at run time
              DarrylBurke
              Cross posted
              [http://www.coderanch.com/t/511032/java/java/Add-jar-file-Java-load]
              [http://www.java-forums.org/advanced-java/32711-add-jar-file-java-load-path-run-time.html]

              db

              edit
              [http://snippets.dzone.com/posts/show/3574]

              Edited by: DarrylBurke