Java: System.getenv("APPDATA") returns null, now what?
I开发者_开发知识库 just found out that when a Java program is launched on Windows via "Run As..." (from the executable's context menu) as a different user, a call to System.getenv("APPDATA")
will return null
, rather than the usual application data folder.
Now, I'm kind of a noob when it comes to Windows folders and such, so my question is, how should I deal with this situation when my program has to be able to store its data somewhere in order to work correctly? More specifically, if System.getenv("APPDATA")
returns null
, am I even allowed to store any data at all, and if so, where? Thanks in advance!
Okay, I solved my own problem using JNA:
com.sun.jna.platform.win32.Shell32Util.getFolderPath(int nFolder)
where nFolder = 0x001a
for the application data folder and nFolder = 0x001c
for the local application data folder. On Windows XP, the output is
C:\Documents and Settings\[UserName]\Application Data
C:\Documents and Settings\[UserName]\Local Settings\Application Data
Most importantly, this does not return null
when the program is launched via Run As
.
Yes you are always allowed to store data. You can use temp directory to store you data. You can get temp directory by System.getProperty("java.io.tmpdir");
.
Also if you set an environment variable in windows, I noticed that java doesn't pick it up until I restarted the OS.
精彩评论