File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes Add a jar file to Java load path at run time Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Add a jar file to Java load path at run time" Watch "Add a jar file to Java load path at run time" New topic

Add a jar file to Java load path at run time

mario fern

Joined: Sep 21, 2010
Posts: 1

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


Here is my class

import java.sql.Connection;
import java.sql.DriverManager;

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[])


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("");

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

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

System.out.println("BEFORE CONNECTION");
Connection conn =
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! -->
In Exception Block -- Failed.
java.sql.SQLException: No suitable driver
at java.sql.DriverManager.getConnection(
at java.sql.DriverManager.getConnection(
at com.tdbfg.tdsecurities.kasper.admin.aboutkasper.JarFileLoader1.main(
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

First of all welcome to the JavaRanch.

Please UseCodeTags when posting code. It makes it so much easier to read. You can edit your post by pressing the editbutton.
Make sure that the file is on the classpath and then use Class.forName("");

"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Darryl Burke

Joined: May 03, 2008
Posts: 5115

Welcome to the Ranch! but BeForthrightWhenCrossPostingToOtherSites



luck, db
There are no new questions, but there may be new answers.
I agree. Here's the link:
subject: Add a jar file to Java load path at run time
It's not a secret anymore!