• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Add a jar file to Java load path at run time

 
mario fern
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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");
 
Darryl Burke
Bartender
Posts: 5148
11
Java Netbeans IDE Opera
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic