I'm trying to make my program save its state in the location set by %APPDATA%
when the user is using Windows. However, System.getProperty("temp.dir");
does not return that. How do I get the value of the %APPDATA%
variable in Windows, for the purpose of state saving?
Asked
Active
Viewed 2.9k times
9

Ky -
- 30,724
- 51
- 192
- 308
-
Possible duplicate http://stackoverflow.com/questions/1198911/how-to-get-local-application-data-folder-in-java – Peter Svensson Feb 10 '12 at 22:34
-
Better duplicate is http://stackoverflow.com/questions/585534/what-is-the-best-way-to-find-the-users-home-directory-in-java – Raymond Chen Feb 11 '12 at 15:04
2 Answers
28
Use System.getenv()
System.getenv("APPDATA")
But I think
System.getProperty("user.home")
should be preferred even though it's not exactly the same thing because it is more portable.
-
What happens if you try `System.getenv("APPDATA")` on a non-Windows OS? Given that `APPDATA` is Windows-specific, I would imagine that some error occurs. – DaveTheMinion Dec 21 '16 at 21:18
-
-
1if you are trying to get the best native integration with the environment then using the "user.home" property is a poor substitute to (Windows) APPDATA or (Free Desktop) XDG_DATA_HOME XDG_CONFG_HOME – Zixradoom Nov 19 '20 at 18:57
9
APPDATA is a Windows specific environment variable that gives you the location where application specific data is stored, so if you are not looking to write platform independent code, you can just do System.getenv("APPDATA");

Steve McLeod
- 51,737
- 47
- 128
- 184

IceMan
- 1,398
- 16
- 35