Working directory for a Java program?
I have a Swing app which uses a text file for saving/loading configuration options.
I can load/save just fine when I launch the program from the IDE -- but when I copy the JAR file (in the DIST folder) to a given location and run it there, it seems to be using some strange directory -- certainly not the folder the .JAR is residing in.
Any ideas?
Matthew
[380 byte] By [
CathInfoa] at [2007-11-27 8:03:05]

Well, a little more information on your load/save code would be fine.Anyways, oftentimes directories default to System.getProperty("user.dir") which is not the directory in which the jar resides, but the directory from which the java command was invoked.
I just did a system-wide search for the config file my program creates --
I only found one: in the logged-in user's HOME directory.
But here's the funny part -- the home directory might be the "current dir" where all relative filenames are saved -- but why isn't it ALSO the place where all relative filenames are SOUGHT FOR?
In other words, if I create 5 files, they will all be in /home/user
If I TRY TO OPEN 5 files, the program will report an error, even if they exist in /home/user
That makes no sense! Are there TWO current directories?
Matthew
> I just did a system-wide search for the config file
> my program creates --
>
> I only found one: in the logged-in user's HOME
> directory.
>
> But here's the funny part -- the home directory might
> be the "current dir" where all relative filenames are
> saved -- but why isn't it ALSO the place where all
> relative filenames are SOUGHT FOR?
>
> In other words, if I create 5 files, they will all be
> in /home/user
> If I TRY TO OPEN 5 files, the program will report an
> error, even if they exist in /home/user
>
> That makes no sense! Are there TWO current
> directories?
>
> Matthew
Not likely. Post the code that you use to write and read the files.
private void vLoadConfigFile() {
FileReader fr = null;
String strFullPath = "";
char[] cTemp;
boolean ynStripSepChar = false;
try {
strFullPath = "email.cfg";
File ffile = new File (strFullPath);
fr = new FileReader(ffile);
BufferedReader bis = new BufferedReader(fr);
cTemp = bis.readLine().toCharArray();
cSepChar = cTemp[0];
strSourceDirectory = bis.readLine();
}
catch (java.io.IOException ex) {
vSaveConfigFile();
}
finally {
try {
if (fr != null) {
fr.close();
}
}
catch (java.io.IOException ex) {
}
}
}
private void vSaveConfigFile() {
FileWriter outputStream = null;
String strFullPath = "";
String strOutputFile = "email.cfg";
int nIter = 0;
Integer nTotal = 0;
String s = "";
try {
strFullPath = strOutputFile;
outputStream = new FileWriter(strFullPath);
outputStream.write(String.valueOf(cSepChar) + "\n");
outputStream.write(strSourceDirectory + "\n");
}
catch (java.io.IOException ex) {
}
finally {
try {
if (outputStream != null) {
outputStream.close();
}
}
catch (java.io.IOException ex) {
}
}
}
There are a number of paths depending on how you ask for a file/information.
Some methods search the classpath (and all contained jars)
some use the directoy java was started from.
Here are a list of standard System.properties
http://www.mindspring.com/~mgrand/java-system-properties.htm
System.getProperty(user.home);
should give you the users home directory.
matfud
Of course, since this is a Java app, I was hoping it would run well in Windows and MacOS as well as Linux.Will Java have a "good answer" for that query, regardless of what platform the program is run?Matthew
If you want to save and load prefs in a platform-neutral manner, the simplest thing is java.util.prefs.If you just need to read, then put the file on the classpath and use Class.getResource or getResourceAsStream.
jverda at 2007-7-12 19:45:12 >

If you were talking to me then:
System.getProprety("user.home");
Will always return a value. Most OS's you are likely to encounter have a concept of users with thier own storage space. In those that don't (i.e embedded systems) it normally points to a location that the single user (there is only one) points to. However you are unlikely to be able to run Swing apps on most embedded systems.
If it fails you could always use the "java.io.tmpdir" ( a random tmp directory) or the "user.dir" (the dir the person was in when they executed the program)
I remember a while back that there was a proposal for a mechanism to allow storage of configuration for programs (and users of them (i.e. the registry on windows or hidden files on linux)). I can't find references to it at the moment though. I'm not sure if it made it into jdk .5 or not.
matfud
> I remember a while back that there was a proposal for
> a mechanism to allow storage of configuration for
> programs (and users of them (i.e. the registry on
> windows or hidden files on linux)). I can't find
> references to it at the moment though. I'm not sure
> if it made it into jdk .5 or not.
java.util.prefs
jverda at 2007-7-12 19:45:12 >

I'm confused. The code posted by OP should actually use the same directory for his load/save ops, shouldn't it?
@OP:
It is not a good idea to have catch blocks that do silently cosume catched exceptions (i.e. do nothing to report them back to the user) - even more so if your code does not behave the way you want it to behave.
> I'm confused. The code posted by OP should actually> use the same directory for his load/save ops,> shouldn't it?If he chooses to explicitly use a file, yes.If he uses the prefs facility, there may not even be a file involved.
jverda at 2007-7-12 19:45:12 >
