Results 1 to 6 of 6
  1. #1
    marioneil is offline Member
    Join Date
    Sep 2010
    Posts
    2
    Rep Power
    0

    Default Add a jar file to Java load path at run time

    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.Ja rFileLoader1.main(JarFileLoader1.java:61)

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,397
    Rep Power
    25

    Default

    ClassNotFoundException: com.ibm.db2.jcc.DB2Driver
    Is the jar file with that class on the classpath?

  3. #3
    marioneil is offline Member
    Join Date
    Sep 2010
    Posts
    2
    Rep Power
    0

    Default

    No it is not.
    I need to load the driver dynamically.

    I am having some other issues on an app server and I need to load the jar file and driver dynamically to overwrite an already loaded driver.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,397
    Rep Power
    25

    Default

    Look at the ClassLoader class and the URLClassLoader class

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,234
    Rep Power
    19

  6. #6
    chyrl is offline Senior Member
    Join Date
    Mar 2010
    Location
    Manila, Philippines
    Posts
    257
    Rep Power
    5

    Exclamation

    Could I use the implementation of ClassLoader and URLClassLoader to solve my thread?
    Every project, package, class, method, variable, syntax, algorithm, etc.
    are registered in my memory bank. Thanks to this thread.

Similar Threads

  1. Replies: 3
    Last Post: 08-23-2011, 12:46 PM
  2. file load
    By myka in forum Advanced Java
    Replies: 1
    Last Post: 04-03-2010, 08:42 PM
  3. File path validation in Java
    By aks123 in forum Advanced Java
    Replies: 2
    Last Post: 10-07-2009, 04:38 PM
  4. how can i give a file path to java stream ?
    By sheckoo in forum New To Java
    Replies: 2
    Last Post: 11-05-2008, 08:42 AM
  5. Load class file which resides in local path c:\
    By praveen.kb in forum Advanced Java
    Replies: 2
    Last Post: 08-12-2008, 01:55 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •