behaviour of getResource()

Discussion in 'Java' started by Andrew Thompson, Oct 27, 2006.

  1. 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.
    Andrew Thompson, Oct 27, 2006
    #1
    1. Advertising

  2. Andrew Thompson

    hiwa Guest

    Andrew Thompson wrote:
    > 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.
    hiwa, Oct 27, 2006
    #2
    1. Advertising

  3. 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.
    Andrew Thompson, Oct 27, 2006
    #3
  4. Andrew Thompson

    hiwa Guest

    Andrew Thompson wrote:

    > a) Why?
    > b) Where is it documented?

    API documentation of Class.getResource() method.
    hiwa, Oct 27, 2006
    #4
  5. hiwa wrote:
    > Andrew Thompson wrote:
    >
    > > a) Why?
    > > b) Where is it documented?

    > 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.
    Andrew Thompson, Oct 27, 2006
    #5
  6. Andrew Thompson

    hiwa Guest

    Andrew Thompson wrote:
    > 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>
    hiwa, Oct 27, 2006
    #6
  7. Andrew Thompson

    Chris Uppal Guest

    Andrew Thompson wrote:

    > * 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
    Chris Uppal, Oct 27, 2006
    #7
  8. Chris Uppal wrote:
    > Andrew Thompson wrote:
    >
    > > * 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...


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

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

    Andrew T.
    Andrew Thompson, Oct 27, 2006
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Manish Hatwalne

    getResource()

    Manish Hatwalne, Feb 9, 2004, in forum: Java
    Replies:
    7
    Views:
    5,950
    Andrew Thompson
    Feb 10, 2004
  2. David Favro

    ClassLoader.getResource() failure

    David Favro, Jun 19, 2004, in forum: Java
    Replies:
    1
    Views:
    24,976
    Jim Sculley
    Jun 19, 2004
  3. Replies:
    0
    Views:
    1,020
  4. Todd
    Replies:
    5
    Views:
    25,878
    Raymond DeCampo
    Jul 23, 2005
  5. Replies:
    0
    Views:
    2,895
Loading...

Share This Page