Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: resource URLs broken in JWS



Doug Zwick wrote:

rslt = this.getClass().getClassLoader().getResource(name);

Another subtlety is that getClassLoader() may return null, if the class was loaded by the primeval ClassLoader().


The Class method getResource() knows this and acts accordingly:

 - - - -
   public java.net.URL getResource(String name) {
        name = resolveName(name);
        ClassLoader cl = getClassLoader0();
        if (cl==null) {
            // A system class.
            return ClassLoader.getSystemResource(name);
        }
        return cl.getResource(name);
    }
 - - -

Also, Class.getClassLoader() may be restricted by security policies, while Class.getResource() is not (its code pasted above).

For details, see the source for Class.getClassLoader().

In general, I'd use the following, unless there's a good reason not to and you're taking a null ClassLoader possibility into account.

 rslt = this.getClass().getResource(name);

  -- GG

_______________________________________________
Do not post admin requests to the list. They will be ignored.
Java-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/java-dev/email@hidden

This email sent to email@hidden


Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.