🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.jbuilder.java.api
Article #1813

Re: (How to) Change the *default* classloader?

#1813
From: Nabin Thomas
Date: Wed, 19 Sep 2001 16:43
176 lines
7174 bytes

Heres another way you can use.

instead of directly loading you main class of the program use an
intermediate class teh loads the actual main class of your program. When
loading the actual Mainclass you should use the
class.forname(classname,classloader) format. From now onwards the
default classloader associated with the actual class ( and all classes
that it loads ). A sample code will be look like this


//ProxyClass.java
public class ProxyClass{
	public static void main(String args[]){
		URL[] urls={ new URL("file:///c:/classes/"),new
URL("file:///d:/plugins") }
		ClassLoader customLoader=new URLClassLoader(urls);
		Class mainClass=Class.forName("mypackage.MainClass",customLoader);
		mypackage.MainClass application=mainClass.newInstance();
		application.main(args);
	}
}

/// mypackage/MainClass.java
package mypackage;
public class MainClass{
	public MainClass(){
		//....
	}
	public void main (String args[]){
		Object
customloadedobject=Class.forName("myclasses.AClass").newInstance();
		// this will work if myclasses.AClass is accessible from the 
		// classpaths specfified in the 		
		// classpath for the proxy class
	}
}






Try this. I hope this will work with my concept of the working of
classloaders.


Holger Stratmann wrote:

> Hi Nabin,
>
> thanks for the hint, but unfortunately, setContextClassLoader() doesn't "do"
> anything!
> All it does is provide a classloader that threads CAN then access (by calling
> getContextClassLoader()). It does not even change the behavior of
> Class.forName, much less what I wanted to do :-((
>
> Nabin Thomas wrote:
>
> > The java.land.Thread class has some method to set the default classloader
> > for that thread.
> > Setting the default classloader of the main thread may probably work
> >
> > May be like
> >
> > class Fooo{
> >     public static void main (String args[]){
> >         URLClassLoader[] utls={ "file://c:/custompath/tomclasses.jar" };
> >         ClassLoader currentLoader=Thread.getContextClassLoader();
> >         Thread.currentThread().setContextClassLoader(new
> > URLClassLoader(urls),currentLoader);
> >            //................................
> >     }
> > //......................
> > }
> >
> > Holger Stratmann wrote:
> >
> > > Hello everybody,
> > >
> > > I've just started experimenting with ClassLoaders and many things
> > > already work fine.
> > > My problem is: I would like my ClassLoader to ALWAYS get the FIRST shot
> > > at loading a class!
> > > Then if it doesn't find it or decides that it doesn't care about that
> > > class, it can call the "normal" system class loader.
> > >
> > > For you to understand what I'm doing:
> > > I want to reload the classes for certain packages during runtime. From
> > > what I've read so far, I need to dispose the ClassLoader that loaded the
> > > classes, in order to dispose the "Class"es.
> > > I do have a ClassLoader that does this. However, subsequent calls to
> > > "Class.forName(...)" always use the classloader that was used to load
> > > that class (the calling class).
> > >
> > > Example: (Problem specification)
> > > My class loader handles "com.me.myunloadablepackage". I use my
> > > ClassLoader to load/define a class in that package. Fine. One of those
> > > classes calls "Class.forName(...)". Still fine, calls MY ClassLoader.
> > > Then I use my classloader to load com.me.somepackage.someclass. My
> > > classloader looks at it and calls super (which loads via "normal" class
> > > loader). Now THIS new class doesn't know/care about my classloader at
> > > all and uses the standard loading mechanism for Class.forName(...).
> > >
> > > I know that I "could"/should get rid of normal
> > > 'Class.forName("classname");' calls, but I can't. I want to change the
> > > classloader for an existing application without adjusting 30 something
> > > source files (that's a rough estimate). Also, I don't want to add this
> > > to the programming guidelines ("don't call Class.forName()" ??!).
> > >
> > > I couldn't find "getClass().SETSystemClassLoader()" *g*
> > >
> > > Any ideas? Where's the missing piece of the puzzle?
> > >
> > > P.S.: I wouldn't want to load EVERY class myself (which would solve the
> > > problem!), because we have 45 jar-Files in our path. I'm kind of afraid
> > > my own classloader would deliver *ehem* - a little less performance than
> > > the "normal" one.
> > > --
> > >
> > >                        \\///
> > >                       ('O O')
> > > --------------------ooO-(_)-Ooo--------------------
> > > Holger Stratmann
> > > mailto:Holger@cheerful.com
> > > ---------------------------------------------------
> >
> > --
> >
> >         ***********************************
> > ***************************************************
> > ******************* Web Card **********************
> > ***************************************************
> >
> > Name :   Nabin Thomas
> >
> > e-mail :   nabinthomas.DONOTSPAM@hotmail.com
> >
> > Web :   http://nabin.topcities.com
> >      http://nabinthomas.net
> >
> > Feel free to contact me thru e-mail or MSN Mssenger
> > if required but DONOT SPAM
> >
> > ***************************************************
> >  ***********************************
>
> --
>
>                        \\///
>                       ('O O')
> --------------------ooO-(_)-Ooo--------------------
> Holger Stratmann
> mailto:Holger@cheerful.com
> ---------------------------------------------------

--

        ***********************************
***************************************************
******************* Web Card **********************
***************************************************

Name :   Nabin Thomas

e-mail :   nabinthomas.DONOTSPAM@hotmail.com

Web :   http://nabin.topcities.com
     http://nabinthomas.net

Feel free to contact me thru e-mail or MSN Mssenger
if required but DONOT SPAM

***************************************************
 ***********************************
Technical Details
Message-ID: <3BA8CB22.AF737902@hotmail.com>
Path: arc1-ca.pugleaf.net!archiver1.google.com!newsfeed.google.com!sn-xit-02!sn-xit-03!supernews.com!newsswitch.lcs.mit.edu!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!fu-berlin.de!news.boerde.de!dnews
References: <3B9F5750.AFC14A93@cheerful.com> <3BA21369.103CC668@hotmail.com> <3BA5C339.C93D4C2D@cheerful.com>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 1089d0,2ebd18d9e54d1eb
X-Google-Attributes: gid1089d0,public
X-Google-ArrivalTime: 2001-09-19 10:03:46 PST
Message-ID: <3BA8CB22.AF737902@hotmail.com>
Date: Wed, 19 Sep 2001 22:13:14 +0530
From: Nabin Thomas <nabinthomas.DONOTSPAM@hotmail.com>
X-Mailer: Mozilla 4.75 [en] (Win98; U)
X-Accept-Language: en
MIME-Version: 1.0
Newsgroups: borland.public.jbuilder.java.api
Subject: Re: (How to) Change the *default* classloader?
References: <3B9F5750.AFC14A93@cheerful.com> <3BA21369.103CC668@hotmail.com> <3BA5C339.C93D4C2D@cheerful.com>
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
NNTP-Posting-Host: 202.169.140.151
X-Trace: dnews 1000918379 202.169.140.151 (19 Sep 2001 09:52:59 -0700)
Lines: 177
Path: arc1-ca.pugleaf.net!archiver1.google.com!newsfeed.google.com!sn-xit-02!sn-xit-03!supernews.com!newsswitch.lcs.mit.edu!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!fu-berlin.de!news.boerde.de!dnews