This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes I/O and Streams and the fly likes Are pathname relative to actual code location (versus possibilities in classpath) ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "Are pathname relative to actual code location (versus possibilities in classpath) ?" Watch "Are pathname relative to actual code location (versus possibilities in classpath) ?" New topic
Author

Are pathname relative to actual code location (versus possibilities in classpath) ?

Nicolas Laurent
Greenhorn

Joined: Apr 06, 2010
Posts: 2
I have the following file layout (your typical eclipse layout) :
project
--images
-----img.png
--bin
----pkg1
------Main.class
----pkg2
------ImageLoader.class

Now image ImageLoader loads an image using the path "images/img.png". This will run fine in eclipse, but I'd like to be able to run this code via the command line without breaking eclipse compatibility. It seems that using something like "java pkg1/Main" from the bin directory will seek "bin/images/img.png" which of course does not exist. I tried adding the project directory to the classpath, without success.

So, is my intuition right and are pathnames relative to the location where to code actually resides and not to all possible locations in the classpath ?
Also, how could I use the java command to successfully run the program without breaking eclipse compatibility ?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19654
    
  18

Move the images folder inside your bin folder, then use Class.getResource(AsStream) or ClassLoader.getResource(AsStream). If you put the images folder inside your source folder and refresh the project it will be copied to the bin folder automatically by Eclipse. This will ensure that the images will not be thrown away when the bin folder is rebuilt.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Nicolas Laurent
Greenhorn

Joined: Apr 06, 2010
Posts: 2
Ok, thanks.
 
jQuery in Action, 2nd edition
 
subject: Are pathname relative to actual code location (versus possibilities in classpath) ?
 
Similar Threads
Image doesnot get displayed in an Eclipse OSGi Project
Build phpbb2JForum from source?
Background Image Problem in External CSS
How to run a java project in command line.
Loading Images