🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

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

Re: changing class path at run time

#167
From: George X
Date: Wed, 29 Mar 2006 20:31
68 lines
3811 bytes
Shankar Unni <shankarunni@netscape.net> wrote:
>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..

Paul & Shankar,

Please excuse my obtuseness.

I think I have incorporated your suggestions as in the code below.  However, DriverManager.getConnection still throws a SQLException of "No suitable driver".

Paul, to answer your question, yes, the call to cld.loadClass does return a non-null reference.

Thanks.
George

    try {
      TestSys testSys = new TestSys();  // an instance of this class
      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, testSys.getClass().getClassLoader());
      Class cl = cld.loadClass("oracle.jdbc.driver.OracleDriver");
      Object driverObj = cl.newInstance();
      Connection connection = DriverManager.getConnection("jdbc:oracle:thin:@STPDB1P:1521:STP20P1", "fo", "fo");
    } catch (Exception ex) {
        ex.printStackTrace();
    }
Technical Details
Message-ID: <442afca7$1@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!g2news1.google.com!news3.google.com!news4.google.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> <4429e83e$1@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 X" <me@here.com>
Sender: "George X" <me@here.com>
Reply-To: "George X" <me@here.com>
Subject: Re: changing class path  at run time
Newsgroups: borland.public.jbuilder.java.language
X-User-Info: 63.72.235.5     
References: <442953d2$1@newsgroups.borland.com> <44296071$1@newsgroups.borland.com> <4429630f@newsgroups.borland.com> <4429a3e5@newsgroups.borland.com> <4429e83e$1@newsgroups.borland.com>
NNTP-Posting-Host: 207.105.83.66
Message-ID: <442afca7$1@newsgroups.borland.com>
Date: 29 Mar 2006 13:31:19 -0700
X-Trace: newsgroups.borland.com 1143667879 207.105.83.66 (29 Mar 2006 13:31:19 -0700)
Lines: 71
Path: arc1-ca.pugleaf.net!g2news1.google.com!news3.google.com!news4.google.com!news2.volia.net!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail