How to get local application data folder in Java?

Possible duplicate:
What is the cross-platform way of getting the local application data directory path?

I am looking for a way to get the location of the data folder of a local application, 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 would like to have is something like this in .NET:

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

Is there a way to do this without calling the SHGetSpecialFolderLocation Windows shell API?

+43
java windows shell
Jul 29 '09 at 9:01
source share
7 answers

Reading the Shell Folders registry key has been deprecated since Windows 95. The registry key contains a note "Do not use this registry key. Instead, use SHGetFolderPath or SHGetKnownFolderPath". I had to find this on a hard way in Vista where all keys were missing, except for a warning note.

https://stackoverflow.com/a/4648323/2123163 https://stackoverflow.com/questions/128298/how-to-create-a-string-in-javascript/232832#2306327

+9
Jan 15 '10 at 21:17
source share
 System.getenv("APPDATA") 

(apparently there is no env variable for the Local Settings folder, but this will give you the Application Data folder)

+68
Jul 29 '09 at 9:08
source share

what about the next

 String dataFolder = System.getenv("LOCALAPPDATA"); 

I have a situation where it is NOT under "user.home"

+6
Dec 01 '11 at
source share

I would like to use the following two methods:

 String dataFolder = System.getenv("APPDATA"); String dataFolder = System.getProperty("user.home") + "\\Local Settings\\ApplicationData"; 
+3
Jul 27 2018-11-11T00:
source share

You can read the path from the registry: HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders\* where * is one of these 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. These sample paths relate to the English installation of Windows XP.

+1
Jul 30 '09 at 9:48
source share

I decided 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; } 
0
Mar 17 '11 at 2:59 a.m.
source share

One could start the process to request a key, and then analyze the output:

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

Honestly, I would be more likely to use JNA or JNI.

-one
Jul 30 '09 at 10:44
source share



All Articles