behaviour of getResource()

A

Andrew Thompson

As far as I understand, this code should print the URL
to the source, twice.

<sscce>
import java.net.URL;

public class WhereAmI {

static String name = "WhereAmI.java";

public WhereAmI() {
System.out.println( getURL(this, name) );
}

public static URL getURL(Object o, String address) {
return o.getClass().getResource(address);
}

public static void main(String[] args) {
Object o = new Object();
System.out.println( getURL(o, name) );
WhereAmI wai = new WhereAmI();
}
}
</sscce>

However, the output I am getting is ..

null
file:/D:/projects/.../WhereAmI.java

Why does the first invocation of getResource(), from main()
using a generic 'Object' fail? As I understand the JavaDocs,
either form should work.

Andrew T.
 
H

hiwa

Andrew said:
As far as I understand, this code should print the URL
to the source, twice.

<sscce>
import java.net.URL;

public class WhereAmI {

static String name = "WhereAmI.java";

public WhereAmI() {
System.out.println( getURL(this, name) );
}

public static URL getURL(Object o, String address) {
return o.getClass().getResource(address);
}

public static void main(String[] args) {
Object o = new Object();
System.out.println( getURL(o, name) );
WhereAmI wai = new WhereAmI();
}
}
</sscce>

However, the output I am getting is ..

null
file:/D:/projects/.../WhereAmI.java

Why does the first invocation of getResource(), from main()
using a generic 'Object' fail? As I understand the JavaDocs,
either form should work.

Andrew T.
I think class loader used is different for user class and for core API
class.
 
A

Andrew Thompson

hiwa wrote:
....
I think class loader used is different for user class and for core API
class.

(A couple of trivial changes to the source, later..)
Thanks! Yes, the two classloaders are *different*,
but that leads me back to ..
a) Why?
b) Where is it documented?

[ I am getting the impression I'll want to slap myself
when I finally understand this. ]

Andrew T.
 
A

Andrew Thompson

hiwa said:
API documentation of Class.getResource() method.

Ohhh.. you mean the bit that says..
"This method delegates to this object's class loader.
If this object was loaded by the bootstrap class
loader, the method delegates to
<link>ClassLoader.getSystemResource(java.lang.String)</link>. "?
...which goes on to say..
"Find a resource of the specified name from the
search path used to load classes. "

Consider me slapped (slightly).

But.. why is "WhereAmI.java" (or the "WhereAmI.class",
for that matter) *not* on the 'search path used to load
classes'? That would be 'the classpath', right?
The classpath (as I understand that statement to mean)
includes 'the current directory' of the "WhereAmI.java"
source as well as the default package "WhereAmI.class".

I just do not get what part is telling me why the bootstrap*
(or any other) classloader will not find these resources that
(as far as I understand) are on the classpath.

* Is there something (else) I missed about the 'bootstrap'**
classloader that says it is for 'core API' classes/resources
only?

** <http://www.google.com.au/search?q=definition+bootstrap>
...oh - those first few definitions are very much more
focused on the *initial* binaries needed to start the
thing being discussed (usually OS's) than I had
taken 'bootstrap' to mean ..
My definition of 'bootstrap' when applied to an OS is
that it might include the stuff in the 'Start-Menu'
(speaking Windows'ese), whereas those definitions
of 'bootstrap' would seem to exclude such extra cruft.

In that sense, it could be implied that the use of the word
'bootstrap' in itself, implies only J2SE core classes?
My confusion was compounded by misunderstanding
the meaning of 'bootstrap'.

Consider me /thoroughly/ slapped.. ;-)

Andrew T.
 
H

hiwa

Andrew said:
I just do not get what part is telling me why
I think it is this part:
<Q>resources associated with a given class are implemented by the
defining class loader of the class</Q>
 
C

Chris Uppal

Andrew said:
* Is there something (else) I missed about the 'bootstrap'**
classloader that says it is for 'core API' classes/resources
only?

I'm a bit shaky on classloaders, but I /think/ the following is true...

The bootstrap classloader is the one that is (for Sun's JVMs) implemented in
C++ code and hardwired into the JVM. It does not behave much, if at all, like
an instance of java.lang.Classloader -- and indeed is represented at the
Java level by a null value (which is why another name for it is "the null
classloader").

It has many interesting features; one of the least interesting (but most
relevant here) is that it does not use the classpath at all. The explicit
classpath is searched by the "application classloader", which /is/ an instance
of java.lang.Classloader; and it is that classloader which loads the "main"
class of the application.

-- chris
 
A

Andrew Thompson

Chris said:
I'm a bit shaky on classloaders, but I /think/ the following is true...

Thanks for the extra details. More grist for the mill..

Thanks also to hiwa for the short, very astute, explanations.

Andrew T.
 

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,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top