How to get local application data folder in Java?

Viewed 105125

Possible Duplicate:
What is the cross-platform way of obtaining the path to the local application data directory?

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 Answers
System.getenv("APPDATA")

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

Reading the "Shell Folders" registry key is deprecated starting from Windows 95. 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.

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

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.

Related