URLClassLoader interesting behaviour...

D

dushkin

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.
 
T

Tom Hawtin

dushkin said:
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
 
D

dushkin

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?
 
D

dushkin

Tom Hawtin כתב:
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

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

Andrew Thompson

dushkin wrote:
Tom said:
...
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?
 
D

dushkin

Andrew Thompson כתב:
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?

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

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!
 
A

Andrew Thompson

dushkin wrote:
...
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?
 
D

dushkin

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...
 
A

Andrew Thompson

dushkin wrote:
...
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.

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

Message posted via JavaKB.com
http://www.javakb.com/Uwe/Forums.aspx/java-general/200704/1
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,120
Latest member
ShelaWalli
Top