Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / January 2006

Tip: Looking for answers? Try searching our database.

Class.getResource path resolution

Thread view: 
kempshall - 03 Jan 2006 21:10 GMT
Can somebody explain how the Class.getResource method constructs its
absolute name? In particular, I have a directory setup like this:
root
|____classes
|       |______myPackage
|                  |__________myApp.class
|____img
       |______myImage.gif

I run my application with classes as my current working directory,
using the command java -classpath . myPackage.myApp, and I want to use
the getResource method to load myImage.gif like this: Image myImg =
Toolkit.getDefaultToolkit().getImage(getClass().getResource(/path/to/myImage.gif))....
but everything I've tried putting into /path/to/myImage.gif results in
some sort of Resource Not Found error. According to the Java
documentation, I should use something like
getResource(/../../img/myImage.gif) or without the leading slash,
getResource(../../../img/myImage.gif), but neither of those work. Can
anybody help me figure out what will work?

Thanks,

Jay
Robert - 04 Jan 2006 02:16 GMT
When we do that we put the file somewhere under the classes dir and the
use "/filename" to find it.  Hope this helps.
Chris Smith - 04 Jan 2006 03:39 GMT
> root
> |____classes
[quoted text clipped - 13 lines]
> getResource(../../../img/myImage.gif), but neither of those work. Can
> anybody help me figure out what will work?

Nothing will work.  Resources need to be placed within the classpath,
which means somewhere in the classes directory.  Move it there, and your
problem is solved.

Signature

www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.