Loading jdbc jars and classes in the application default classloader dynamically..  
Author Message
Irfan Bondre





PostPosted: 2005-10-21 7:07:00 Top

java-programmer, Loading jdbc jars and classes in the application default classloader dynamically.. In our application, the JDBC jar files are stored in pre configured
location. say c:\server\libs\1.jar, 2.jar

We don't want to include these jars file in the class path. However,
whenever, we receive a jdbc request, we want to be able to load all the
jars located in this location and then do the basic jdbc call
sequence...

e.g. Class.forname("com.mydriver.SQLServerDriver");
Connection conn = DriverManager.getConnection(
"jdbc:mydriver:sqlserver://sqlserver1:1433;DatabaseName=Mydb;User=****t;Password=*****"
);


How ca i do this?

I tried the following code but doesn't help... I keep getting the
java.sql.SQLException: No suitable driver error. After somedebugging, I
found that the Drivermanager uses some getCallerClassLoader and that
doesn't match the class loader of com.mydriver.SQLServerDriver.

How do i make the loading using the default class loader at the same
time including those jar files?


String x = "kjds";
File f = new File("D:/server/libs");
File[] jarFiles = f.listFiles(new ExtentionFilter("jar"));

URL[] urls = new URL[jarFiles.length+1];
int i=0;
for(;i< jarFiles.length;i++){
try{
urls[i] = jarFiles [i].toURL();
}catch(Exception ex){}
}

ClassLoader z = new URLClassLoader(urls,
x.getClass().getClassLoader());


try
{
//Class DriverClass =
z.loadClass("com.mydriver.SQLServerDriver");
//Driver dd = (Driver)DriverClass.newInstance();
// DriverManager.registerDriver(dd);
Driver x =
(Driver)Class.forName("com.actuate.actuatedd.jdbc.sqlserver.SQLServerDriver",
true, z);
String cnstr =
"jdbc:mydriver:sqlserver://sqlserver1:1433;DatabaseName=Mydb;User=****t;Password=*****"

Connection conn = DriverManager.getConnection( cnstr );
}
catch (Exception e)
{
e.printStackTrace();
}

 
John Currier





PostPosted: 2005-10-21 8:02:00 Top

java-programmer >> Loading jdbc jars and classes in the application default classloader dynamically.. Irfan Bondre wrote:
> In our application, the JDBC jar files are stored in pre configured
> location. say c:\server\libs\1.jar, 2.jar
>
> We don't want to include these jars file in the class path. However,
> whenever, we receive a jdbc request, we want to be able to load all the
> jars located in this location and then do the basic jdbc call
> sequence...
>
> e.g. Class.forname("com.mydriver.SQLServerDriver");
> Connection conn = DriverManager.getConnection(
> "jdbc:mydriver:sqlserver://sqlserver1:1433;DatabaseName=Mydb;User=****t;Password=*****"
> );
>
>
> How ca i do this?
>
> I tried the following code but doesn't help... I keep getting the
> java.sql.SQLException: No suitable driver error. After somedebugging, I
> found that the Drivermanager uses some getCallerClassLoader and that
> doesn't match the class loader of com.mydriver.SQLServerDriver.
>
> How do i make the loading using the default class loader at the same
> time including those jar files?

How about something like this:

URLClassLoader loader = new
URLClassLoader((URL[])classpath.toArray(new URL[0]));
Driver driver = (Driver)Class.forName(driverClass, true,
loader).newInstance();

where classpath is a List of URLs built from File objects. See
SchemaSpy's net.sourceforge.schemaspy.Main.getConnection() for a
working example.

John
http://schemaspy.sourceforge.net

 
Irfan Bondre





PostPosted: 2005-10-22 0:36:00 Top

java-programmer >> Loading jdbc jars and classes in the application default classloader dynamically.. That won't work, because, the Drivermanager checks to see if the
calling application's class loader can load the class. But it won't
because default class loader is different than the one whcih loaded the
class loader.

If you see my code is similar to yours.

Irfan.

 
 
John Currier





PostPosted: 2005-10-22 11:59:00 Top

java-programmer >> Loading jdbc jars and classes in the application default classloader dynamically.. Irfan Bondre wrote:
> That won't work, because, the Drivermanager checks to see if the
> calling application's class loader can load the class. But it won't
> because default class loader is different than the one whcih loaded the
> class loader.
>
> If you see my code is similar to yours.
>
> Irfan.

Yes, your code is similar, but mine works and does what you're trying
to do. The only difference is where the collection of jar names comes
from. Maybe the problem is related to the parent classloader that
you're specifying.

I hope you use more meaningful variable names in 'real' code.

John
http://schemaspy.sourceforge.net