Article View: borland.public.jbuilder.java.api
Article #1359Re: Changing the default class loader.
From: Stefan Bouynov
Date: Thu, 31 Jan 2002 07:52
Date: Thu, 31 Jan 2002 07:52
55 lines
3161 bytes
3161 bytes
"Hanan Hermon" <hananh@radcom.co.il> wrote in message
news:3c550667$1_2@dnews...
| Hi,
|
| I want the system to use my ClassLoader, instead of the default defined
for
| it.
| I know how to write my ClassLoader, and how to use it for loading classes.
|
| But I have two problems:
| 1) The system always tries to use the default ClassLoader to load classes,
| before using mine (as it should according to the documentation), but I
don't
| want it do the standard thing, and use the default first, I want it to use
| mine first.
Yes, system classloader is the default classloader and it is used to load
classes.zip/tools.jar packages (simply because without java.lang.Object you
cannot load any class at all), but for other packages you could control that
via the CLASSPATH variable.
When starting, what you want to start, you could set CLASSPATH to point to
classes.zip(for JDK 1.1)/tools.jar(for JDK 1.2 and above) only (e.g. set
CLASSPATH=<JDK dir>\lib\tools.jar), overriding the system classpath if
necessary. Then you could write a simple wrapper for your app, which will be
used only for setting the correct classpath to your classloader, and loading
the main class of the app (through the reflection mechanism), like this:
Class myMainClass = myClassLoader.loadClass("my.mainclass.name");
Method mainMethod = myMainClass.getMethod("main", new Class[]
{String[].class});
Object obj = myMainClass.newInstance();
mainMethod.invoke(obj, null);
This way you will provide that only the base classes will be loaded through
the system classloader, and all your classes - through your custom
classloader, since your main class holds the classpath, pointing their
location.
| 2) I know only how to read classes that are in regular files, and I would
| like to read classes from zip or jar files, and that I don't know how to
do.
Well, it is really easy to get the InputStream from a zip/jar entry. You
will only need the java.util.zip package (since JAR files are actually ZIP
files, but with a different extention, and a manifest file in it). You can
create a ZipFile class, and then query for the entries (zipFile.entries()),
or get an InputStream for an entry
(zipFile.getInputStream(zipFile.getEntry("entry name"))). After that, you
should know what to do.
HTH
Regards,
Stefan
Technical Details
Message-ID:
<3c58f5d5_2@dnews>Path:
arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-02!supernews.com!newsfeed.direct.ca!look.ca!newsfeed.wirehub.nl!PeerFeed.KWeb.on.ca!dnewsReferences:
<3c550667$1_2@dnews>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 1089d0,144532415003ca26 X-Google-Attributes: gid1089d0,public X-Google-ArrivalTime: 2002-01-30 23:48:57 PST From: "Stefan Bouynov" <bouynov@yahoo.com> Newsgroups: borland.public.jbuilder.java.api References: <3c550667$1_2@dnews> Subject: Re: Changing the default class loader. Date: Thu, 31 Jan 2002 09:52:15 +0200 Lines: 57 X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2600.0000 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2600.0000 NNTP-Posting-Host: 212.56.7.50 Message-ID: <3c58f5d5_2@dnews> X-Trace: dnews 1012463061 212.56.7.50 (30 Jan 2002 23:44:21 -0800) Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-02!supernews.com!newsfeed.direct.ca!look.ca!newsfeed.wirehub.nl!PeerFeed.KWeb.on.ca!dnews