Class.getResource path resolution

K

kempshall

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
 
R

Robert

When we do that we put the file somewhere under the classes dir and the
use "/filename" to find it. Hope this helps.
 
C

Chris Smith

kempshall said:
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?

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.

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

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top