• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

getResource()

 
Ranch Hand
Posts: 296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Is there any difference between these two:

Thanks,
Drew
 
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Like Hertz, not exactly. Here is what the API docs say for Class.getResource(String):

This method delegates the call to its class loader, after making these changes to the resource name: if the resource name starts with "/", it is unchanged; otherwise, the package name is prepended to the resource name after converting "." to "/". If this object was loaded by the bootstrap loader, the call is delegated to ClassLoader.getSystemResource.

Here is the 1.4.1 implementation for Class.getResource(String):

As you can see the ClassLoader is retrieved directly from a package-private native method.
 
Drew Lane
Ranch Hand
Posts: 296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Still not sure I totally understand this.
Let me ask this:
When I call a method such as getResource(String) does the classloader convert this to an absolute path (eg. C:\myfile\mygif.gif) dependent on the OS?
Drew
 
Ranch Hand
Posts: 3178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Drew Lane:
Still not sure I totally understand this.
Let me ask this:
When I call a method such as getResource(String) does the classloader convert this to an absolute path (eg. C:\myfile\mygif.gif) dependent on the OS?
Drew


Yes, Drew, that depends on the platform that the program is running...
I guess I've made u understand totally on it...
:roll:
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you supply an absolute path in the first place, no conversion is necessary; it just works. This can be useful. Another method worth looking into it getSystemResource(). It uses the system CLASSPATH as the basis of its search. Which is often what you want, unless you've put your resources in the same directory as the class file for the class that uses them. (Which is not a bad idea, but it's not the only way to do things.)
 
It's just like a fortune cookie, but instead of a cookie, it's pie. And we'll call it ... tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic