views:

4321

answers:

5

I'm looking for a way to get the location of the local application data folder, which is a special Windows folder, in Java. Unfortunately, the following only works for English versions of Windows XP with default settings:

System.getProperty("user.home") + "\\Local Settings\\Application Data"

What I'd like to have is something like this in .NET:

System.Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData)

Is there a way to do it without having to call SHGetSpecialFolderLocation of the Windows Shell API?

+7  A: 
System.getenv("APPDATA")

(there seems to be no env variable for the "Local Settings" folder, but this will give you the 'Application Data' folder)

Ryan Fernandes
just understand that the APPDATA environmental variable is a windows only construct, but the app in question is java, so it should ostensibly be cross platform.
Chii
The questioner is asking for something which is "a special Windows folder". So I'm not convinced this needs to be cross platform
Brian Agnew
It doesn't need to be cross platform. I'm just looking for a way to get the location of this special Windows folder for the current user.
smf68
About the APPDATA environment variable: I know about that one, but need the local Application Data folder which doesn't always seem to be inside the Application Data folder. Just noticed there is a LOCALAPPDATA, I'll check if that's the right one and if it's always available.
smf68
Just checked, LOCALAPPDATA is not available in Windows XP, unfortunately.
smf68
A: 

What do you need it for?

There are more Java-y ways to store persistent information, such as the Preferences API or using the temporary file mechanisms (possibly combined :)

Christoffer
My application needs to read configuration data from another application (which I cannot move) that's stored in this special folder.
smf68
Ah, then it's more clear why you want this.
Christoffer
+4  A: 

You could read the path from the registry: HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders\* where * is one of those Keys:

  • Local AppData (C:\Documents and Settings\USER\Local Settings\Application Data)
  • Local Settings (C:\Documents and Settings\USER\Local Settings)
  • AppData (C:\Documents and Settings\USER\Application Data)

Note: Those example paths are from an english Windows XP installation

Gregor
I'm now reading it with the SWT Win32 Extension (http://feeling.sourceforge.net/) and it works fine. Thanks!
smf68
+2  A: 

It would be possible to spawn a process to query the key and then parse the output:

REG QUERY "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders" /v "Local AppData"

Honestly, though, I'd be more inclined to use JNA or JNI.

McDowell
I also think that the way to go would be calling the native function of Windows, as it should be the one which does not change in the near future.
Gregor
+2  A: 

Reading the "Shell Folders" registry key is deprecated starting from Windows Vista. The registry key contains a note saying "!Do not use this registry key. Use the SHGetFolderPath or SHGetKnownFolderPath instead." I had to find this out the hard way on a Vista system where all the keys were missing except for the warning note.

This related stackoverflow answer solves this problem on Windows using JNA, which is the solution I'm currently using.

Frederik