getResource()

  • Thread starter Manish Hatwalne
  • Start date
M

Manish Hatwalne

I need little help regarding the method getResource of class "CLass". I
sthere a way to find out and print the actuall resource the class is trying
to get?
When I try this -

URL u = SomeClass.class.getResource("../../images/pic.gif");

I get URL "u" as null. I know what is happening but I would just like to
print the exact path of the resource that this code is trying to get. How
can I do this?

TIA,
- Manish
 
A

Andrew Thompson

Manish said:
I need little help regarding the method getResource of class "CLass".
I sthere a way to find out and print the actuall resource the class
is trying to get?
When I try this -

URL u = SomeClass.class.getResource("../../images/pic.gif");

I get URL "u" as null. I know what is happening but I would just like
to print the exact path of the resource that this code is trying to
get. How can I do this?

It is not so much 'trying to get'
as 'searching'. The path is the
current classpath, and may involve
several differnet paths.


Try leaving off the '../../' bit, it will
probably work.
 
H

hiwa

Manish Hatwalne said:
I need little help regarding the method getResource of class "CLass". I
sthere a way to find out and print the actuall resource the class is trying
to get?
When I try this -

URL u = SomeClass.class.getResource("../../images/pic.gif");

I get URL "u" as null. I know what is happening but I would just like to
print the exact path of the resource that this code is trying to get. How
can I do this?

TIA,
- Manish

The correct argument might be: "/images/pic.gif".
The argument for the Class#getResource() method can't be platform
filesystem path name. Re-read the javadoc.
 
M

Manish Hatwalne

hiwa said:
"Manish Hatwalne" <[email protected]> wrote in message

The correct argument might be: "/images/pic.gif".
The argument for the Class#getResource() method can't be platform
filesystem path name. Re-read the javadoc.

I have read it and as I said in my post I know why it is not working. But I
would like to print the "url", the getResource() method will construct. Is
it possible to print it somehow???

TIA,
- Manish
 
A

Andrew Thompson

You obviously cannot print somthing that is
'null'
...I know what is happening but I would just

I have read it and as I said in my post I know why it is not working.
But I would like to print the "url", the getResource() method will
construct. Is it possible to print it somehow???

Once you _have_ a definitive URL,
yes, of course you can print it.
....you _are_ aware of URL.toString()??

I do not understand, either you have not
read the JavaDocs carefully, or you have
not expressed your problem clearly enough
for us to answer.
 
M

Manish Hatwalne

???
Once you _have_ a definitive URL,
yes, of course you can print it.
...you _are_ aware of URL.toString()??

I do not understand, either you have not
read the JavaDocs carefully, or you have
not expressed your problem clearly enough
for us to answer.

I think it is the later. Let me try again -

I have pacakge say com.some_package
and I have this code in the file which is in this package.
URL u = SomeClass.class.getResource("../../images/pic.gif");

With this I get URL as "null", obviously I can't invoke toString on null.
When I do this, it works and gives me a proper URL -
URL u = SomeClass.class.getResource("com/some_package/images/pic.gif");

So I am aware of the problem, due to dots not getting resolved the way I
anticipate them.
And I want to understnad what exactly is the resource the code -

SomeClass.class.getResource("../../images/pic.gif");

is trying to get. Is it
com/some_package../../images/pic.gif ? (After reading JavaDoc, I think ths
is what it is trying by prepending package name to it).

If yes, then I would like to print the same. How????
Hope I made it clear this time.

TIA,
- Manish
 
M

Manish Hatwalne

Actually, it would have been much better if only the code for method
getResource() -
SomeClass.class.getResource("../../images/pic.gif");

had thrown a runtime exception saying it didn't find the specified resource
with the resource string. But it simply returns null! :-(

- Manish
 
A

Andrew Thompson

Manish said:
???

I think it is the later. Let me try again -

I have pacakge say com.some_package
and I have this code in the file which is in this package.
URL u = SomeClass.class.getResource("../../images/pic.gif");

With this I get URL as "null", obviously I can't invoke toString on
null. When I do this, it works and gives me a proper URL -
URL u =
SomeClass.class.getResource("com/some_package/images/pic.gif");

So I am aware of the problem, due to dots not getting resolved the
way I anticipate them.
And I want to understnad what exactly is the resource the code -

SomeClass.class.getResource("../../images/pic.gif");

is trying to get.

If the getResource() works as I think, it
would check for such '../' early and simply
return 'null' without even looking.
[ 'I look into the source code/JLS gurus'*
feel free to correct me here. ]

It is important to note that you should not
need to express it either of these ways. I
suspect you must not have seen my first post
on this (this is my 3rd post in this thread)
as I see no reference to you trying what I
suggested in 1st post.
SomeClass.getClass().getResource("images/pic.gif");

That should work even if 'images' directory
is deep inside a package structure. In fact,
if pic.gif is the _only_ gif of that name on the
classpath, this should work as well..
SomeClass.getClass().getResource("pic.gif");
.. Is it
com/some_package../../images/pic.gif ? (After reading JavaDoc, I
think ths is what it is trying by prepending package name to it).

* If you are really that interested how Sun
codes the core classes, are running late model
Windows (I run XP) and have the 'full Java install',
try 'Search...'ing the Java directory (e.g. C://j2sdk1.4.2_02/)
for Class.java, it should be found inside the ..
'whatever it is'.jar (shrugs, Winblows finds
it, but does not report the name of the jar??)

A cursory look at the source (all I have time
for) seems to suggest to me that Sun is only
prepending the package name if you start
the string "/images/pic.gif".
If yes, then I would like to print the same. How????
Hope I made it clear this time.

You are asking where Java looks
in that situation? That sounds to me
like asking how you spell 'Java' in the
fourth dimension, Manish. ;-)

With a well formed resource name it
would check all the directories/jars
listed on the class path, not just 'a'
place.

I feel your question (as I now
understand it) is best answered with
'use a valid path before worrying
about such minor technicalities'.

(shrugs) I hope that makes it a bit
more clear for you, and that it helps,
but if you are that keen to find out
what Java does, examine the source.
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top