Article View: borland.public.jbuilder.java.language
Article #251Re: changing class path at run time
From: George
Date: Tue, 28 Mar 2006 20:00
Date: Tue, 28 Mar 2006 20:00
62 lines
3120 bytes
3120 bytes
"Paul Furbacher [TeamB]" <pfurbacher@mac.com> wrote:
>George wrote:
>> "George" <me@here.com> wrote:
>>> I want to load JDBC drivers on demand. The user tells me the
>>> driver class name and where the jar files are located and my app
>>> loads the driver. Or so I would like. The following code does
>>> not work:
>>>
>>> try { System.setProperty("java.class.path", path + ";" +
>>> "C:\\MyDrivers\\ojdbc14.jar" );
>>> Class.forName("oracle.jdbc.OracleDriver"); } catch (Exception ex)
>>> { ex.printStackTrace(); }
>>>
>>>
>>> The code works if I include the jar file on the java command line
>>> invocation. What am I doing wrong?
>>>
>>> TIA George
>>>
>>> Never mind. I just found out about URLClassLoader.
>
>Yes. There are several open source projects (e.g., OR mappers)
>which have tools that provide you with this capability. One
>which comes to mind is objectstyle.org's Cayenne Modeler.
>You might want to study their code to see if you can enhance
>what you already have accomplished. (It's always good to see
>how other folks approach a problem.)
>
>
>--
>
>
>Paul Furbacher (TeamB)
>
> Save time, search the archives:
> http://info.borland.com/newsgroups/ngsearch.html
>
> Is it in Joi Ellis's Faq-O-Matic?
> http://www.visi.com/~gyles19/fom-serve/cache/1.html
>
> Finally, please send responses to the newsgroup only.
> That means, do not send email directly to me.
> Thank you.
I guess I spoke too soon.
I'm having trouble going the URLClassLoader route. The code below succeeds in loading the Oracle jdbc driver...however the call to DriverManager.getConnection() returns saying "No suitable driver"...
George
try {
File f = new File(System.getProperty("user.dir") + "\\ojdbc14.jar");
URL url = new URL("file:///"+ f.getAbsolutePath());
URL [] urlList = new URL[1];
urlList[0] = url;
URLClassLoader cld = new URLClassLoader(urlList);
Class cl = cld.loadClass("oracle.jdbc.OracleDriver");
Connection connection = DriverManager.getConnection("jdbc:oracle:thin:@STPDB1P:1521:STP20P1", "fo", "fo");
} catch (Exception ex) {
ex.printStackTrace();
}
Technical Details
Message-ID:
<4429a3e5@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!g2news1.google.com!news2.google.com!news4.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mailReferences:
<442953d2$1@newsgroups.borland.com> <44296071$1@newsgroups.borland.com> <4429630f@newsgroups.borland.com>Show Full Headers
Complete Article Headers
X-Google-Thread: f74b3,93e0a6dd5feccd47 X-Google-Attributes: gidf74b3,public X-Google-Language: ENGLISH,ASCII-7-bit From: "George" <me@here.com> Sender: "George" <me@here.com> Reply-To: "George" <me@here.com> Subject: Re: changing class path at run time Newsgroups: borland.public.jbuilder.java.language X-User-Info: 63.72.235.4 References: <442953d2$1@newsgroups.borland.com> <44296071$1@newsgroups.borland.com> <4429630f@newsgroups.borland.com> NNTP-Posting-Host: 207.105.83.66 Message-ID: <4429a3e5@newsgroups.borland.com> Date: 28 Mar 2006 13:00:21 -0700 X-Trace: newsgroups.borland.com 1143579621 207.105.83.66 (28 Mar 2006 13:00:21 -0700) Lines: 64 Path: arc1-ca.pugleaf.net!g2news1.google.com!news2.google.com!news4.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail