views:

62

answers:

4

I'm writing a java application. Currently this produces a file called "trace.txt" in the same directory the .jar file lives. This can sometimes cause issue with file permissions, eg writing to C:\Program Files\Foo\trace.txt.

After a bit of research on SO it seems the general opinion is to log to Application Data\Foo\ for the given user. This won't be the same location for other OS's.

What's the best practice in this situation? I'm using java.util.logging.

A: 

If you're running as a web application, then the webserver logs directory (e.g. /usr/local/apache/logs) shouldn't give you any permissions issues; otherwise you might have to "pick and mix" based on platform.

Mark Baker
Just my opinion, the user is asking on cross platform of his java program either as a server/web/ application or standalone application. You don't know.From "...Application Data..." mentioned in the question, it seems a standalone application.But any how, even it is an web application, we may answer on solving is problem of where is the suitable place to store a log file.
ttchong
typo: "the user is asking on cross platform setup of his java program"
ttchong
+2  A: 

You could always use the USER_HOME, identified by ~ in Linux or

System.getProperty("user.home"); in java.

You'll see lots of apps have their own .myappname folder, and put config files, logs in there.

This will map to /home/user/.myapp in linux or C:/Documents and settings/user/.myapp in windows, and should always be writable.

lucas1000001
*"... USER_HOME, identified by ~ in Linux"* - that is so wrong! 1) It is the shell (e.g. bash) that understands "~" ... not Linux. The shell is not Linux specific - it also runs on Bsd, Cygwin, classif UNIX, etc, etc. 2) In fact, "~" just expands to the value of the "HOME" environment variable, and that environment variable is available in more contexts than "~" works in.
Stephen C
That's a pretty anal comment to be fair. It was just a quick example, not a specific definition. I understand that Linux is just a kernel and not the software that runs on top of it, and that bash is just one of many potential shells. I would be careful picking meaningless holes in things - you sound like one of those guys who give *nix enthusiasts a bad name.
lucas1000001
A: 

If your java program require "installation" (it may be just set up some configuration files, move you binary to certain place etc), then it is perfectly fine to handle permission stuff (in what ever platform) during installation or other platform specific stuff for whatever log files, configurations, caches user settings etc. Installation usually implies user has to run the installation script/program with superuser privilege. So, you can do so.

Generally if your application store user specific data you may store it to /home//. in Linux like system and Users\ttchong\AppData in Window 7 for example (I am not very familiar in Window, correct me if I am wrong). And your code also need to handle the user path to allow multiple users have different settings of your program.

Else, if it is program information like log files that you don't mind to dump together I still prefer the way you do it now, store it with the same directory of your jar file or a little better in a log folder in the same directory by using the "installation" trick.

ttchong
A: 

If this is a "real" log or trace file, I would write it to the Default temp file path that you can get via the java.io.tmpdir system property.

Pascal Thivent