This week's book giveaway is in the OCPJP forum. We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line! See this thread for details.
A while ago I was trying to work through an i/o chapter of a Java book I was reading and was having problems figuring out where my "current directory" was so that I could tell java where to find my file to be read. A kindly JavaRanch feller gave me this code:
This was very helpful for the exercise I was working on, however I'd like to know how java determines what its "current directory" should be set to for the future. Currently I'm using eclipse and it seems eclipse does some behind the scenes stuff that I'm not aware of. Could anyone please explain how this works or point me to an article that covers this?
There is a bit in the File API which I presume you have read already. I can only presume that the "current directory" is wherever you have navigated your shell/terminal/command prompt to. Eclipse tends to set up a file called workspace, which it regards as part of the path to its current directory.
As you see in the code, Java uses the System Property "user.dir" to determine the current working directory. What that often is system dependent.
You can control the current working directory that Java uses by setting the "user.dir" path at startup:
This is what Eclipse does. It sets the current path to the directory it uses to deploy the project to. You can configure what directory Eclipse uses by going to Run -> Open Run Dialog, go to the Arguments tab, and and choose 'Other' at the bottom of the tab under 'Working Directory', then use the File System button to find the directory you want to act as the working directory.