How to get local application data folder in Java? [duplicate]
Asked Answered
R

7

54

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?

Rummy answered 29/7, 2009 at 9:1 Comment(4)
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 :)Varus
My application needs to read configuration data from another application (which I cannot move) that's stored in this special folder.Rummy
Ah, then it's more clear why you want this.Varus
The same question but cross-platform: #11114474Plaintiff
B
13

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.

Bouley answered 15/1, 2010 at 21:17 Comment(1)
The key was deprecated starting from Windows 95. the "Shell Folders" key exists solely to permit four programs written in 1994 to continue running on the RTM version of Windows 95 blogs.msdn.com/b/oldnewthing/archive/2003/11/03/55532.aspxMisanthropy
C
85
System.getenv("APPDATA")

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

Carboloy answered 29/7, 2009 at 9:8 Comment(9)
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.Eli
The questioner is asking for something which is "a special Windows folder". So I'm not convinced this needs to be cross platformTransalpine
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.Rummy
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.Rummy
Just checked, LOCALAPPDATA is not available in Windows XP, unfortunately.Rummy
Note that the APPDATA environment variable is not available in applications that has been started via "Run As ..."Dennisedennison
APPDATA is not available?Bevin
FWIW this returns null on macOS Big Sur.Erick
what if we want it to be applicable for all users...? is there any solution for it instead of specific 1 single user location?Bevin
C
22

what about the following

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

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

Collegian answered 1/12, 2011 at 21:11 Comment(0)
B
13

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.

Bouley answered 15/1, 2010 at 21:17 Comment(1)
The key was deprecated starting from Windows 95. the "Shell Folders" key exists solely to permit four programs written in 1994 to continue running on the RTM version of Windows 95 blogs.msdn.com/b/oldnewthing/archive/2003/11/03/55532.aspxMisanthropy
S
3

I would like to use the following two ways:

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

String dataFolder = System.getProperty("user.home") + "\\Local Settings\\ApplicationData";
Stoughton answered 27/7, 2011 at 13:36 Comment(0)
T
1

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

Tapley answered 30/7, 2009 at 9:48 Comment(3)
I'm now reading it with the SWT Win32 Extension (feeling.sourceforge.net) and it works fine. Thanks!Rummy
Do not read the registry key directly. It does not do what you think it does.Rozanne
And in fact, though the Local Settings actual folder is still kind of there for backwards compatibility, the registry value is no more at all since Vista.Marjoram
P
0

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;
}
Proulx answered 17/3, 2011 at 14:59 Comment(2)
Dunno why this was downvoted so much, it seems solid enough to me. A bit... excessive, but solid.Amersfoort
Maybe because System.getenv("APPDATA") should be the same?Indra
D
-1

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.

Dabster answered 30/7, 2009 at 10:44 Comment(1)
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.Tapley

© 2022 - 2024 — McMap. All rights reserved.