 |
 |
Pathnames in Java
|
 |
|
 |
|
Mac Elite
Join Date: Jul 2002
Location: Right Here
Status:
Offline
|
|
I'm writing a Mac OS X application in Java. In this program, I load images using the Toolkit.getDefaultToolkit().getImage() method, and I load sounds using java.io.File(). In both of these cases, the program only works if I specify the full pathname to the image or sound file - for example, /Users/me/ProgramLocation/Images/Image.jpg. This is a problem because I may want to distribute the program someday, and the exact pathname will vary depending on where users install the program. I would like to be able to specify a relative pathname - something like "./Images/Image.jpg," which is relative to the location of the program. However, this does not work. Does anyone know of a way to specify relative pathnames that does work? Thanks.
|
|
|
| |
|
|
|
 |
|
 |
|
Mac Elite
Join Date: Sep 2000
Location: Edmond, OK USA
Status:
Offline
|
|
Originally posted by Anomalous:
I'm writing a Mac OS X application in Java. In this program, I load images using the Toolkit.getDefaultToolkit().getImage() method, and I load sounds using java.io.File(). In both of these cases, the program only works if I specify the full pathname to the image or sound file - for example, /Users/me/ProgramLocation/Images/Image.jpg. This is a problem because I may want to distribute the program someday, and the exact pathname will vary depending on where users install the program. I would like to be able to specify a relative pathname - something like "./Images/Image.jpg," which is relative to the location of the program. However, this does not work. Does anyone know of a way to specify relative pathnames that does work? Thanks.
Actually, relative paths DO work, the problem is that you don't know what they are relative to. You have a few options here:
1 - explicitly set a property to indicate home, then work with absolute paths:
Code:
java -Dhome=/Applications/MyApp.app/ whatever.class
//Then in your app use this:
File home = new File(System.getProperty("home"));
2 - If you bundle your app in a .app bundle, the runtime provides pointers to the install directory, and additionally defines user.dir (the cwd) as a specific sub directory of the bundle (I can't remember where).
3 - If you really want to go relative, just print out the user.dir property (or simply do new File("").getAbsolutePath()) to find out where it is pointing.
|
|
|
| |
|
|
|
 |
|
 |
|
Mac Elite
Join Date: Sep 2000
Location: Edmond, OK USA
Status:
Offline
|
|
BTW, the path is not relative to where the application is, it is whatever the current working directory is when the app is launched. E.G., if I type:
Code:
cd /var
java -cp ~/classes exec
The current dircetory is /var and all file paths are relative to that (except for absolute paths).
Code:
cd /var/tmp
java -cp ~/classes exec
The current dircetory is /var/tmp
In both these cases the classes are stored in ~/classes.
|
|
|
| |
|
|
|
 |
|
 |
|
Fresh-Faced Recruit
Join Date: Oct 2001
Status:
Offline
|
|
Do not hardcode paths, its so very windows (and unix). Best thing to do is to have the images in your classpath (like in com.foo.*) and read the resources from there (and when you have jared you application). It should work with any type of resources (except fonts i think for which you have to wait for 1.4.1)
(Last edited by Hvdvrk; Dec 5, 2002 at 05:48 PM.
)
|
|
|
| |
|
|
|
 |
|
 |
|
Mac Elite
Join Date: Sep 2000
Location: Edmond, OK USA
Status:
Offline
|
|
Originally posted by Hvdvrk:
Do not hardcode paths, its so very windows (and unix). Best thing to do is to have the images in your classpath (like in com.foo.*) and read the resources from there (and when you have jared you application). It should work with any type of resources (except fonts i think for which you have to wait for 1.4.1)
That's an unnecessary concern. If he was to hardcode '/usr/local/images' or 'C  winnt\images' then he'd be shooting himself (and others) in the foot. However, as long as he can tailor the installation directory independently, then whether he places them in the classpath (requiring the batch file or compiler to place them appropriately) or has a relative path to where it is installed, will achieve the same goal. On thing to remember with the classpath, however, is that it is read only, and you generally don't want to access entries as files, and you can't iterate over the contents like you can with directories, and it isn't as obvious where things should go.
|
|
|
| |
|
|
|
 |
|
 |
|
Mac Elite
Join Date: Jul 2002
Location: Right Here
Status:
Offline
|
|
Thanks for your ideas. You got me thinking along the right lines, and I was able to figure out the answer I was looking for using my Java book.
|
|
|
| |
|
|
|
 |
 |
|
 |
|
|
|
|
|

|
|
 |
Forum Rules
|
 |
 |
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is Off
|
|
|
|
|
|
 |
 |
 |
 |
|
 |
|