Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / April 2007

Tip: Looking for answers? Try searching our database.

URLClassLoader interesting behaviour...

Thread view: 
dushkin - 18 Apr 2007 17:36 GMT
Hello All...

I am trying to load an xml file out of a jar file. In a directory I
have 2 jar files, both have the same named xml file.

Please read the following code:

*****************************************************************
URL[] arr = {new URL("http://dummy.com")};
arr[0] = new URL("file://.//" + m_sComponentName + ".jar");

jcl=new URLClassLoader(arr);
urlCommandsFile =
jcl.getResource(Common_ST.instance().COMMANDS_FILE_NAME);

*****************************************************************

evertything works virtually fine, without exceptions, but when I
inspect urlCommandsFile.getPath(), I see that the path is of the
neighbour jar file and not of the loaded jar!!!

How can it be???

I use eclipse and both jars are inside the project.

Thanks.
Tom Hawtin - 18 Apr 2007 18:05 GMT
> I use eclipse and both jars are inside the project.

Then I guess both classes will already be available, and your new class
loader wont get a look in. You need to separate out the classes to a
different location.

Tom Hawtin
dushkin - 18 Apr 2007 21:22 GMT
> Then I guess both classes will already be available, and your new class
> loader wont get a look in. You need to separate out the classes to a
> different location.
>
> Tom Hawtin

Thanks Tom.

So you mean that I must put each jar in a seperate folder??? Is it
mandatory?
Andrew Thompson - 19 Apr 2007 09:44 GMT
Tom wrote:
>> Then I guess both classes will already be available, and your new class
>> loader wont get a look in. You need to separate out the classes to a
>> different location.
...
> So you mean that I must put each jar in a seperate folder???

No.  Tom did not mean putting the jar's in different
directories, he meant putting the XML files in different
paths within the Jar files.

>..Is it mandatory?

Well.. let me put it this way.

If you had two Jars on the classpath, one with an XML
file at path..
 The1st.jar!/com/mycompany/the.xml
..the second with an XML file at path..
 The2nd.jar!/com/mycompany/the.xml
..how is the classloader supposed to know which
one is required?

Perhaps more relevant, is that your initial code
shows only one jar added to the URLClassLoader
(UCL).  If both jars are on the classpath at runtime,
the UCL is not needed - it should be possible to
obtain resources from them using the default
classloader.  Why are you creating the UCL at all?

Signature

Andrew Thompson
http://www.athompson.info/andrew/

dushkin - 19 Apr 2007 21:23 GMT
Andrew Thompson כתב:

> Well.. let me put it this way.
>
[quoted text clipped - 18 lines]
>
> Message posted via http://www.javakb.com

Thanks Andrew for your reply.
It may be very possible that my method is wrong... Hence I will be
greatful if you could guide me.

I would like to use resources like xml files, JPanel classes, etc.
which has the same names from several jar file, which I will know
their names and number only on runtime.

For example: At run time I have to find several jar files in a certain
folder which all of them have the file aaa.xml.
According to some runtime trigger I will want to load first aaa.xml on
x1.jar and afterwards, according to another trigger aaa.xml from
x4.xml.
Similarily for some same named classes.

What is the correct way to do it?
I thought that I should load first the jar files using URLClassLoader,
and then use GetResource().

Many thanks!
Andrew Thompson - 20 Apr 2007 02:36 GMT
...
>For example: At run time I have to find several jar files in a certain
>folder which all of them have the file aaa.xml.
>According to some runtime trigger I will want to load first aaa.xml on
>x1.jar and afterwards, according to another trigger aaa.xml from
>x4.xml.
>Similarily for some same named classes.

As an aside.  What funcitionality are you trying to
offer to the end user, by these attempts?  Plug-ins
for the program?

Signature

Andrew Thompson
http://www.athompson.info/andrew/

dushkin - 20 Apr 2007 13:56 GMT
Andrew Thompson כתב:

> As an aside.  What funcitionality are you trying to
> offer to the end user, by these attempts?  Plug-ins
> for the program?
>
> -

I am writing an automatic testing application, which is based on a
generic framework, which loads specific testing component jars (at
least this is my intention...).

Now, the jars are consisted of specific resources like internet
protocol commands xml, properties panels for each commands to be
attached into specific places on the GUI framework, etc. .These
resources should be loaded by demand upon user request. The group of
jars is expandable. It is a sort of plugin framework.

For example: If a user wants to build an IMAP test, he will choose the
IMAP menu item, and then I want to show him the list of IMAP commands,
etc. I wouldn't want to have the list of all commands hard coded
inside my code, and also I would like to let the use to expand the
list if he like to.
So I would need to have something like command.xml in IMAP.jar,
HTTP.jar, etc. The same name of file so I will be genric, with of
course differnet contents.

Hope it is clearer now and waiting for your kind advise...
Andrew Thompson - 20 Apr 2007 14:12 GMT
...
>Hope it is clearer now and waiting for your kind advise...

That is much more clear to me, and enough to convince
me that I do not have the skills to answer the question.

( There is a design paradigm for these sorts of things,
and many of the OO experts* are good at answering
these types of questions.  * No, I am just a hack coder,
rather than an OO expert. ;)

I will bow out of this thread now, and hopefully one of
the design experts will wander by very soon, to give
better advice on a good strategy to achieve the goal.

Signature

Andrew Thompson
http://www.athompson.info/andrew/

dushkin - 20 Apr 2007 20:37 GMT
Thank you Andrew.
dushkin - 19 Apr 2007 08:00 GMT
Tom Hawtin כתב:

> > I use eclipse and both jars are inside the project.
>
[quoted text clipped - 3 lines]
>
> Tom Hawtin

It didn't help putting the jars on seperate folders....


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.