使用Java查找用户独立的TEMP目录
当使用用户“LocalService”将 Java 应用程序作为服务运行时,临时目录(“java.io.tmpdir”)指向“c:/windows/temp”(例如)。
运行 Java 应用程序通常会给出“c:/documents and settings/user/local settings/temp”。
当我的应用程序正常运行时,如何确定用户独立的临时文件夹“c:/windows/temp”?
谢谢和问候, 甘德
when running a Java application as service with the user 'LocalService', the temp directory ("java.io.tmpdir") points to 'c:/windows/temp' (for example).
Running a Java application normally gives 'c:/documents and settings/user/local settings/temp' instead.
How can I determine the user independent temp folder 'c:/windows/temp' when my application runs normally?
Thanks and greetings,
GHad
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以:
或者使用 JVM 的命令行参数将此值作为变量传递给您的应用程序,例如
-Dmytempdir=%WINDIR%\temp
正如您所提到的,用户可以更改其中任何一个的值
使用系统变量->环境变量,但我认为它们不会对系统产生任何影响,直到重新启动为止(...?)。
或者...
这可能会非常混乱,我不知道 Preferences 类是否能让您访问您需要读取的密钥。同样,如果用户确实想要更改注册表值,您也无能为力,但我再次怀疑它会在重新启动之前产生任何影响,并且可能不仅仅会对您的应用程序产生影响。
干杯,
--J
You could:
or pass in this value to your app as a variable using a commandline argument to the JVM, e.g.
-Dmytempdir=%WINDIR%\temp
As you mention, the user could change the values of either of these
variables using System -> Environment Variables, but I don't think they'd have any affect on the system until a reboot anyway (...?).
Or...
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment\TEMP
.This would likely have to be quite messy and I don't know if the Preferences class will get you access to the key you'd need to read. Again, there's not much you could do about the user changing the registry value either, if they really wanted to, but again I doubt it would have any affect until after a reboot, and would probably have an impact on more than just your app.
Cheers,
--J
我不确定是否有一种“干净”的方法来做到这一点。
在这种情况下,我可能会专门为 Java 应用程序创建一个目录,并在属性文件中引用它。
I'm not sure there is a 'clean' way of doing this.
In this situation, I would probably create a directory specifically for the Java app and refer to it in a properties file.
Java系统属性java.io.tmpdir只是指向系统变量%TMP%。
对于普通用户 %TMP% 指向 %HOMEPATH%\temp,对于其他帐户 - 可以是另一个路径。
您可以尝试使用 %SYSTEMROOT%\temp 而不是 java.io.tmpdir - %SYSTEMROOT% 指向安装 Windows 的目录。
Java system property java.io.tmpdir just point to system variable %TMP%.
For normal user %TMP% points to %HOMEPATH%\temp, for other account - can be another path.
You can try to use %SYSTEMROOT%\temp instead of java.io.tmpdir - %SYSTEMROOT% points to directory, where windows is installed.
您可以简单地创建自己的临时文件夹,添加使用 deleteOnExit() 方法以确保在应用程序退出时删除此文件夹。
You can simply create your own temporary folder, add use the deleteOnExit() method to ensure this folder will be removed at the exit of your application.