6

I have a Java desktop application that I have written. During the execution I create folders and files at the default path name defined in the system.

Java.io.files clearly states: By default the classes in the java.io package always resolve relative pathnames against the current user directory. This directory is named by the system property user.dir, and is typically the directory in which the Java virtual machine was invoked.

In addition, I am using IzPack to enable installation and shortcuts creation.

When I'm running my application on my XP computer, after the installation I get a desktop shortcut, and the mentioned files and folders creation are at the location that Izpack installed the Jar. which is the expected behavior.

But when I test this out on a Vista machine, the folders and files are created on the desktop! even though the Jar is at the correct location (c:\program files.. etc).

I want those files to be created at the same folder the Jar is in, and most certainly not at the desktop.

Can anyone give me any insights on what is going on here?

uzil24
  • 156
  • 4
  • 13
  • 2
    Writing to c:\program files in Vista needs UAC consent, so I suspect the System uses the desktop as a fallback. – Eugen Rieck Jan 11 '12 at 15:03
  • Is there any way you think I can control the fallback? – uzil24 Jan 11 '12 at 16:27
  • You can still set default java app installation inside %PROGRAMFILES% for core app libraries and such. But, instead of saving your **user/data configurations** any where inside the %PROGRAMFILES% under any Windows OS, how about using `System.getProperty("user.home")` or `System.getenv("USERPROFILE")` with a special folder inside for your java app which it makes it easier for user/app to change something? – ee. Jan 12 '12 at 03:17
  • I think what troubles me the most is the fact that the installer was able to put the application at the "Program Files" location, and yet my application was unable to write to the same location. I know that Izpack uses Windows native code for their installer, which I imagine should explain it, but it's still complicating the whole process. I want the uninstaller to be able to remove the application, and not have some files at the "user.home" location while the Jar is at "user.dir" – uzil24 Jan 12 '12 at 13:40

1 Answers1

1

It's because in Vista/Seven, writing to the Program Files folder requires administrative interference, so JVM looks for the next writable location as a fallback: the Desktop (or the User Documents directory). You can easily determine the User home directory in a unified manner on all OSs, though, which is way better than just letting the JVM pick a -- hopefully -- reasonable location.

Since this is a known bug for JVM on Windows, if that doesn't help, the fallback is to check the System Environment variable USERPROFILE which should point at the correct user home folder:

String userHome = System.getenv("USERPROFILE");
Milad Naseri
  • 4,053
  • 1
  • 27
  • 39
  • I see. I did not take this under consideration. One possible problem of using the User home directory is the Uninstall issue. I'm not sure how can I make those files deleted in that case. I know that some applications tend to leave folders behind them. I never liked it. – uzil24 Jan 11 '12 at 16:30
  • 1
    You can easily check to see if the selected folder is writable by attempting to create a temporary file and checking if its parent folder is what you expect it to be, and in case of failure choose another location. – Milad Naseri Jan 11 '12 at 16:48
  • ok, I revised my application so that it uses the "user.dir" instead of the relative path and the result is a disaster. with the desktop shortcut the folders are created on the desktop. with the "all programs" menu shortcut the folders are created at the c:\doc and setting\user location. And when running the jar directly from the c:\program files\app_name location, it runs as it should. using the "user.home" has the same outcome each time on the other hand. how can I a make the application run at the place the jar is? – uzil24 Jan 12 '12 at 19:15
  • According to bug [4787931](http://bugs.sun.com/view_bug.do?bug_id=4787931) this is a known issue. If you are using Java 1.5+, the working solution is to use `System.getenv("USERPROFILE")` which would give you the correct path to user's home directory. Tell me if that helps. (PS: I updated the answer as well, and sorry for the late reply, was at a hiking trip ;-)) – Milad Naseri Jan 14 '12 at 19:04
  • eventually I used this link to solve my probelm: http://stackoverflow.com/questions/320542/how-to-get-the-path-of-a-running-jar-file and used the Jar's path to try and create the folders. if the process has failed (I check it by creating a temp file, as was suggested above) I simply use the "user.home" location. – uzil24 Jan 17 '12 at 16:32
  • Thanks for sharing the answer here. I hope you continue contributing to the developer community here on SO. – Milad Naseri Jan 17 '12 at 20:01