aspose file tools*
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


Win a copy of Java 8 in Action this week in the Java 8 forum!
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
Author

Add a jar file to Java load path at run time

mario fern
Greenhorn

Joined: Sep 21, 2010
Posts: 1
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)
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("com.ibm.db2.jcc.DB2Driver");


"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
Bartender

Joined: May 03, 2008
Posts: 4523
    
    5

Welcome to the Ranch! but BeForthrightWhenCrossPostingToOtherSites
http://www.java-forums.org/advanced-java/32711-add-jar-file-java-load-path-run-time.html

edit
http://forums.sun.com/thread.jspa?threadID=5451006

edit2
http://snippets.dzone.com/posts/show/3574


luck, db
There are no new questions, but there may be new answers.
 
 
subject: Add a jar file to Java load path at run time
 
Similar Threads
class loader
java.sql.SQLException: No suitable driver
No suitable Driver in DB2
jdbc program written in Eclipse3.2.1 to connect DB2
How to add new jar files to class path dynamically at runtime?