File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes Working directory question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Working directory question" Watch "Working directory question" New topic

Working directory question

Micah Wedemeyer
Ranch Hand

Joined: Jun 11, 2001
Posts: 68
I'm having some trouble with portability of one of my applications. When I create, access, and modify my files I use relative path names. I always assumed that the path names were relative to the directory containing the class files.
My .jar file is in directory X. In X there is a subdirectory subdir ( \X\subdir ) containing some files, for example file1.txt. In my program I attempt to access my files in subdir using the following: File f = new File("subdir\file1.txt")
On some machines (mine, a Mac running Mac OS 9) this seems to work, and on others (my friend's, a Mac running Mac OS X) it gives a file not found error. Is there any way to solve this? Are there any web sites that give explicit explanantions of how the JVM resovles file paths?
Any help would be appreciated.
Steve Deadsea
Ranch Hand

Joined: Dec 03, 2001
Posts: 125
Relative files are relative to the working directory. The working directory is *not* the directory that contains the class files. The working directory is the directory from which the program was started. You can usually start the program from any directory by specifying the classpath as a command line argument.
If your classes are packaged in a jar file or served over the internet you still want to be able to find other resources such as translations, images, configuration files, etc that go with your program.
You need to treat these files as "system resources". Programs can accesses resources such as images, propery files, help documents, and web pages that are stored in a jar file along with the program.
I agree. Here's the link:
subject: Working directory question
jQuery in Action, 3rd edition