How do I get the value of Windows' %APPDATA% location variable in Java?
Asked Answered
M

2

9

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?

Maihem answered 10/2, 2012 at 22:31 Comment(2)
Possible duplicate #1199411Pernas
Better duplicate is #586034Thaddeus
A
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.

Appetitive answered 10/2, 2012 at 22:33 Comment(3)
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.Valueless
Great. Thank you!Valueless
if 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_HOMESavaii
C
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");

Castleberry answered 10/2, 2012 at 22:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.