How to get local application data folder in Java?
JavaWindowsShellJava Problem Overview
> 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 http://msdn.microsoft.com/en-us/library/bb762203(VS.85).aspx">SHGetSpecialFolderLocation</a> of the Windows Shell API?
Java Solutions
Solution 1 - Java
System.getenv("APPDATA")
(there seems to be no env variable for the "Local Settings" folder, but this will give you the 'Application Data' folder)
Solution 2 - Java
what about the following
String dataFolder = System.getenv("LOCALAPPDATA");
I have a situation where this is NOT under "user.home"
Solution 3 - Java
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.
Solution 4 - Java
I would like to use the following two ways:
String dataFolder = System.getenv("APPDATA");
String dataFolder = System.getProperty("user.home") + "\\Local Settings\\ApplicationData";
Solution 5 - Java
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
Solution 6 - Java
I resolved in this way
private static File getAppData(){
ProcessBuilder builder = new ProcessBuilder(new String[]{"cmd", "/C echo %APPDATA%"});
BufferedReader br = null;
try {
Process start = builder.start();
br = new BufferedReader(new InputStreamReader(start.getInputStream()));
String path = br.readLine();
// TODO HACK do not know why but I get an extra '"' at the end
if(path.endsWith("\"")){
path = path.substring(0, path.length()-1);
}
return new File(path.trim());
} catch (IOException ex) {
Logger.getLogger(Util.class.getName()).log(Level.SEVERE, "Cannot get Application Data Folder", ex);
} finally {
if(br != null){
try {
br.close();
} catch (IOException ex) {
Logger.getLogger(Util.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
return null;
}
Solution 7 - Java
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.