File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Class.getResource(String) and Class.getResourceAsStream(String) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Class.getResource(String) and Class.getResourceAsStream(String)" Watch "Class.getResource(String) and Class.getResourceAsStream(String)" New topic
Author

Class.getResource(String) and Class.getResourceAsStream(String)

Cameron Park
Ranch Hand

Joined: Apr 06, 2001
Posts: 371
Hi, I have a question about the getResource(String) and getResourceAsStream(String) methods in the Class class. What exacty are the "resources"? Why is the return of getResource(String) a URL?
Thanks.
Vanitha Sugumaran
Ranch Hand

Joined: Apr 11, 2001
Posts: 356
--------------------------------------
Hi,
I have a question about the getResource(String) and getResourceAsStream(String) methods in the Class class. What exacty are the "resources"? Why is the return of getResource(String) a URL?
Thanks.
--------------------------------------------
Hi,
Resource is some data like images, text... that can accessed by the class code.
It returns the URL for reading the resource.
I haven't tried these methods, so I don't have a practical example.
Vanitha
Cameron Park
Ranch Hand

Joined: Apr 06, 2001
Posts: 371
Where do they search? The local drive? The Internet?
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
Per the API:

Finds a resource with a given name. This method returns null if no resource with this name is found. The rules for searching resources associated with a given class are implemented by the * defining class loader of the class.
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.

So if you feed in a string "/myfile.txt" the file is searched for in just the path and classpath directories. If you feed in "myfile.txt" the package name is added and a search is done in exactly the same manner that class files are looked for, therefore the Path and classpath directories are searched.


"JavaRanch, where the deer and the Certified play" - David O'Meara
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Class.getResource(String) and Class.getResourceAsStream(String)
 
Similar Threads
how can i get path of class file in an app.?
Multiple questions
how to specify the path for DocumentBuilder.parse
Image In Executable JAR
location of xsd file