🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.jbuilder.java.language
Article #165

Re: changing class path at run time

#165
From: Shankar Unni
Date: Wed, 29 Mar 2006 01:51
41 lines
2878 bytes
George wrote:

>       URLClassLoader cld = new URLClassLoader(urlList);
>       Class cl = cld.loadClass("oracle.jdbc.OracleDriver");
>       Connection connection = DriverManager.getConnection("jdbc:oracle:thin:@STPDB1P:1521:STP20P1", "fo", "fo");

This is really unlikely to work. Even if you had created "cld" with its 
parent set to ThisClass.getClass().getClassLoader(), you'd still have 
this hierarchy:

     ThisClass  <-     loaded by base class loader

     OracleDriver <-   loaded by "cld" (assuming that ojdbc14.jar is not 
in your regular classpath)

Remember that each class is loaded *in its own classloader* only. It is 
not visible to classes loaded by its parent classloader, or by classes 
loaded by other classloaders unrelated to its classloader.

So, OracleDriver in the above example is only visible to classes loaded 
by "cld", or by other classloaders that are children of "cld".

There's more to what Cayenne is doing than just making cld's parent the 
calling class' classloader. Check carefully to see how they make the 
driver visible.

I strongly suspect that they are doing something like this:

     URLClassLoader cld = new URLClassLoader(urlList);
     Class driverClass = cld.loadClass("oracle.jdbc.OracleDriver");
     // call the default constructor of the driver class
     // using reflection:
     Object driverObj = driverClass.newInstance();

At this point, assuming that oracle.jdbc.OracleDriver has a default 
constructor (should have), you'll see that the driver is now registered, 
and DriverManager.getConnection() should now work.

PS. Check the documentation to see if the preferred class is 
oracle.jdbc.OracleDriver, or oracle.jdbc.driver.OracleDriver. My memory 
is that Oracle now prefers that you use the latter name..
Technical Details
Message-ID: <4429e83e$1@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!g2news1.google.com!news3.google.com!news.glorb.com!news2.volia.net!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail
References: <442953d2$1@newsgroups.borland.com> <44296071$1@newsgroups.borland.com> <4429630f@newsgroups.borland.com> <4429a3e5@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
Date: Tue, 28 Mar 2006 17:51:59 -0800
From: Shankar Unni <shankarunni@netscape.net>
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8) Gecko/20051201 Thunderbird/1.5 Mnenhy/0.7.3.0
MIME-Version: 1.0
Newsgroups: borland.public.jbuilder.java.language
Subject: Re: changing class path  at run time
References: <442953d2$1@newsgroups.borland.com> <44296071$1@newsgroups.borland.com> <4429630f@newsgroups.borland.com> <4429a3e5@newsgroups.borland.com>
In-Reply-To: <4429a3e5@newsgroups.borland.com>
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 7bit
NNTP-Posting-Host: 64.171.57.66
Message-ID: <4429e83e$1@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1143597118 64.171.57.66 (28 Mar 2006 17:51:58 -0700)
Lines: 41
Path: arc1-ca.pugleaf.net!g2news1.google.com!news3.google.com!news.glorb.com!news2.volia.net!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail