Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

user working directory: XP vs Vista

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?

like image 746
uzil24 Avatar asked Jan 11 '12 14:01

uzil24


1 Answers

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");
like image 73
Milad Naseri Avatar answered Oct 20 '22 08:10

Milad Naseri