[英]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. 我刚刚发现当一个Java程序在Windows上通过“Run As ...”(从可执行文件的上下文菜单)作为另一个用户启动时,对
System.getenv("APPDATA")
的调用将返回null
,而不是通常的应用程序数据文件夹
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? 现在,当涉及到Windows文件夹等时,我有点像菜鸟,所以我的问题是,当我的程序必须能够将数据存储在某处以便正常工作时,我应该如何处理这种情况? More specifically, if
System.getenv("APPDATA")
returns null
, am I even allowed to store any data at all, and if so, where? 更具体地说,如果
System.getenv("APPDATA")
返回null
,我甚至可以允许存储任何数据,如果是,那么在哪里? Thanks in advance! 提前致谢!
Okay, I solved my own problem using JNA: 好的,我使用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. 其中
nFolder = 0x001a
表示应用程序数据文件夹, nFolder = 0x001c
表示本地应用程序数据文件夹。 On Windows XP, the output is 在Windows XP上,输出是
C:\\Documents and Settings\\[UserName]\\Application Data
C:\\ Documents and Settings \\ [UserName] \\ Application Data
C:\\Documents and Settings\\[UserName]\\Local Settings\\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
. 最重要的是,当程序通过
Run As
启动时,这不会返回null
。
另外,如果你在windows中设置一个环境变量,我注意到在我重新启动操作系统之前java没有提取它。
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");
您可以通过
System.getProperty("java.io.tmpdir");
获取临时目录System.getProperty("java.io.tmpdir");
. 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.