DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Add A Jar File To Java Load Path At Run Time

02.23.2007
| 84978 views |
  • submit to reddit
        Sometimes it is necessary to amend the class load path at run time. For example, dynamically adding jar files containing user-configurable JDBC data sources. The way this is done is truly monstrous -- the URL Path format was only revealed when a colleague looked into the Java library sources.

import java.net.URL;
import java.io.IOException;
import java.net.URLClassLoader;
import java.net.MalformedURLException;

public class JarFileLoader extends URLClassLoader
{
    public JarFileLoader (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
        {
            System.out.println ("First attempt...");
            Class.forName ("org.gjt.mm.mysql.Driver");
        }
        catch (Exception ex)
        {
            System.out.println ("Failed.");
        }

        try
        {
            URL urls [] = {};

            JarFileLoader cl = new JarFileLoader (urls);
            cl.addFile ("/opt/mysql-connector-java-5.0.4/mysql-connector-java-5.0.4-bin.jar");
            System.out.println ("Second attempt...");
            cl.loadClass ("org.gjt.mm.mysql.Driver");
            System.out.println ("Success!");
        }
        catch (Exception ex)
        {
            System.out.println ("Failed.");
            ex.printStackTrace ();
        }
    }
}
    

Comments

Snippets Manager replied on Tue, 2010/09/21 - 3:23pm

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 package com.tdbfg.tdsecurities.kasper.admin.aboutkasper; 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)

Bedla Czech replied on Fri, 2010/03/26 - 3:53pm

Thanks for snippet. For me was working with one slash in specifying urlPath variable (I have Windows). public void addFile(String path) throws MalformedURLException { String urlPath = "jar:file:/" + path + "!/"; addURL(new URL(urlPath)); } Thank calling cl.addFile("c:/java/testability-explorer/testability-explorer-1.3.2.jar");